src/Pure/context.ML
author wenzelm
Fri, 17 Jun 2005 18:33:21 +0200
changeset 16436 7eb6b6cbd166
parent 15801 d2f5ca3c048d
child 16489 f66ab8a4e98f
permissions -rw-r--r--
added type theory: generic theory contexts with unique identity, arbitrarily typed data, linear and graph development -- a complete rewrite of code that used to be spread over in sign.ML, theory.ML, theory_data.ML, pure_thy.ML;
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
16436
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
     5
Generic theory contexts with unique identity, arbitrarily typed data,
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
     6
linear and graph development.  Implicit ML theory context.
6185
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
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
     9
signature BASIC_CONTEXT =
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    10
sig
16436
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    11
  type theory
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    12
  type theory_ref
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    13
  exception THEORY of string * theory list
6185
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    14
  val context: theory -> unit
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    15
  val the_context: unit -> theory
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    16
end;
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    17
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    18
signature CONTEXT =
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    19
sig
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    20
  include BASIC_CONTEXT
16436
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    21
  (*theory context*)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    22
  val parents_of: theory -> theory list
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    23
  val ancestors_of: theory -> theory list
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    24
  val is_stale: theory -> bool
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    25
  val ProtoPureN: string
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    26
  val PureN: string
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    27
  val CPureN: string
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    28
  val draftN: string
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    29
  val is_draft: theory -> bool
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    30
  val exists_name: string -> theory -> bool
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    31
  val names_of: theory -> string list
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    32
  val pretty_thy: theory -> Pretty.T
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    33
  val string_of_thy: theory -> string
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    34
  val pprint_thy: theory -> pprint_args -> unit
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    35
  val pretty_abbrev_thy: theory -> Pretty.T
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    36
  val str_of_thy: theory -> string
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    37
  val check_thy: string -> theory -> theory
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    38
  val eq_thy: theory * theory -> bool
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    39
  val subthy: theory * theory -> bool
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    40
  val self_ref: theory -> theory_ref
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    41
  val deref: theory_ref -> theory
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    42
  exception DATA_FAIL of exn * string
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    43
  val theory_data: theory -> string list
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    44
  val print_all_data: theory -> unit
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    45
  val copy_thy: theory -> theory
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    46
  val checkpoint_thy: theory -> theory
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    47
  val pre_pure: theory
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    48
  val merge: theory * theory -> theory                     (*exception TERM*)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    49
  val merge_refs: theory_ref * theory_ref -> theory_ref    (*exception TERM*)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    50
  val theory_name: theory -> string
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    51
  val begin_theory: (theory -> Pretty.pp) -> string -> theory list -> theory
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    52
  val end_theory: theory -> theory
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    53
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    54
  (*ML context*)
6185
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    55
  val get_context: unit -> theory option
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    56
  val set_context: theory option -> unit
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    57
  val reset_context: unit -> unit
6238
bd7b4a23118f setmp: theory option;
wenzelm
parents: 6185
diff changeset
    58
  val setmp: theory option -> ('a -> 'b) -> 'a -> 'b
6310
wenzelm
parents: 6261
diff changeset
    59
  val pass: theory option -> ('a -> 'b) -> 'a -> 'b * theory option
wenzelm
parents: 6261
diff changeset
    60
  val pass_theory: theory -> ('a -> 'b) -> 'a -> 'b * theory
6238
bd7b4a23118f setmp: theory option;
wenzelm
parents: 6185
diff changeset
    61
  val save: ('a -> 'b) -> 'a -> 'b
6185
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    62
  val >> : (theory -> theory) -> unit
10914
aded4ba99b88 use_text etc.: proper output of error messages;
wenzelm
parents: 10893
diff changeset
    63
  val ml_output: (string -> unit) * (string -> unit)
8348
ebbbfdb35c84 added use_mltext, use_mltext_theory, use_let, use_setup (from isar_thy.ML);
wenzelm
parents: 6310
diff changeset
    64
  val use_mltext: string -> bool -> theory option -> unit
ebbbfdb35c84 added use_mltext, use_mltext_theory, use_let, use_setup (from isar_thy.ML);
wenzelm
parents: 6310
diff changeset
    65
  val use_mltext_theory: string -> bool -> theory -> theory
ebbbfdb35c84 added use_mltext, use_mltext_theory, use_let, use_setup (from isar_thy.ML);
wenzelm
parents: 6310
diff changeset
    66
  val use_let: string -> string -> string -> theory -> theory
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15531
diff changeset
    67
  val add_setup: (theory -> theory) list -> unit
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15531
diff changeset
    68
  val setup: unit -> (theory -> theory) list
6185
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    69
end;
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    70
16436
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    71
signature PRIVATE_CONTEXT =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    72
sig
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    73
  include CONTEXT
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    74
  structure TheoryData:
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    75
  sig
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    76
    val declare: string -> Object.T -> (Object.T -> Object.T) ->
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    77
      (Object.T -> Object.T) -> (Pretty.pp -> Object.T * Object.T -> Object.T) ->
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    78
      (theory -> Object.T -> unit) -> serial
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    79
    val init: serial -> theory -> theory
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    80
    val print: serial -> theory -> unit
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    81
    val get: serial -> (Object.T -> 'a) -> theory -> 'a
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    82
    val put: serial -> ('a -> Object.T) -> 'a -> theory -> theory
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    83
  end;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    84
end;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    85
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    86
structure Context: PRIVATE_CONTEXT =
6185
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    87
struct
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    88
16436
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    89
(*** theory context ***)
6185
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    90
16436
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    91
datatype theory =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    92
  Theory of
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    93
  (*identity*)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    94
   {self: theory ref option,
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    95
    id: serial * string,
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    96
    ids: string Inttab.table} *
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    97
  (*data*)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    98
  Object.T Inttab.table *
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
    99
  (*history of linear development*)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   100
   {name: string,
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   101
    version: int,
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   102
    intermediates: theory list} *
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   103
  (*ancestry of graph development*)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   104
   {parents: theory list,
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   105
    ancestors: theory list};
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   106
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   107
exception THEORY of string * theory list;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   108
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   109
fun rep_theory (Theory args) = args;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   110
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   111
val identity_of = #1 o rep_theory;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   112
val data_of     = #2 o rep_theory;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   113
val history_of  = #3 o rep_theory;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   114
val ancestry_of = #4 o rep_theory;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   115
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   116
fun make_identity self id ids = {self = self, id = id, ids = ids};
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   117
fun make_history name vers ints = {name = name, version = vers, intermediates = ints};
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   118
fun make_ancestry parents ancestors = {parents = parents, ancestors = ancestors};
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   119
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   120
val parents_of = #parents o ancestry_of;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   121
val ancestors_of = #ancestors o ancestry_of;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   122
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   123
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   124
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   125
(** theory identity **)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   126
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   127
(* staleness *)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   128
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   129
fun eq_id ((i: int, _), (j, _)) = i = j;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   130
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   131
fun is_stale
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   132
    (Theory ({self = SOME (ref (Theory ({id = id', ...}, _, _, _))), id, ...}, _, _, _)) =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   133
      not (eq_id (id, id'))
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   134
  | is_stale (Theory ({self = NONE, ...}, _, _, _)) = true;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   135
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   136
fun vitalize (thy as Theory ({self = SOME r, ...}, _, _, _)) = (r := thy; thy)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   137
  | vitalize (thy as Theory ({self = NONE, id, ids}, data, history, ancestry)) =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   138
      let
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   139
        val r = ref thy;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   140
        val thy' = Theory (make_identity (SOME r) id ids, data, history, ancestry);
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   141
      in r := thy'; thy' end;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   142
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   143
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   144
(* names *)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   145
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   146
val ProtoPureN = "ProtoPure";
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   147
val PureN = "Pure";
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   148
val CPureN = "CPure";
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   149
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   150
val draftN = "#";
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   151
fun draft_id (_, name) = (name = draftN);
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   152
val is_draft = draft_id o #id o identity_of;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   153
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   154
fun exists_name name (Theory ({id, ids, ...}, _, _, _)) =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   155
  name = #2 id orelse Inttab.exists (equal name o #2) ids;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   156
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   157
fun names_of (Theory ({id, ids, ...}, _, _, _)) =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   158
  map #2 (Inttab.dest ids @ [id]);
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   159
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   160
fun pretty_thy thy =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   161
  Pretty.str_list "{" "}" (names_of thy @ (if is_stale thy then ["!"] else []));
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   162
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   163
val string_of_thy = Pretty.string_of o pretty_thy;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   164
val pprint_thy = Pretty.pprint o pretty_thy;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   165
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   166
fun pretty_abbrev_thy thy =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   167
  let
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   168
    val names = names_of thy;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   169
    val n = length names;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   170
    val abbrev = if n > 5 then "..." :: List.drop (names, n - 5) else names;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   171
  in Pretty.str_list "{" "}" abbrev end;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   172
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   173
val str_of_thy = Pretty.str_of o pretty_abbrev_thy;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   174
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   175
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   176
(* consistency *)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   177
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   178
fun check_thy pos thy =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   179
  if is_stale thy then
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   180
    raise TERM ("Stale theory encountered (see " ^ pos ^ "):\n" ^ string_of_thy thy, [])
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   181
  else thy;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   182
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   183
fun check_insert id ids =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   184
  if draft_id id orelse is_some (Inttab.lookup (ids, #1 id)) then ids
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   185
  else if Inttab.exists (equal (#2 id) o #2) ids then
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   186
    raise TERM ("Different versions of theory component " ^ quote (#2 id), [])
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   187
  else Inttab.update (id, ids);
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   188
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   189
fun check_merge thy1 thy2 =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   190
  let
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   191
    val {id = id1, ids = ids1, ...} = identity_of thy1;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   192
    val {id = id2, ids = ids2, ...} = identity_of thy2;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   193
  in check_insert id2 (Inttab.fold check_insert ids2 (check_insert id1 ids1)) end;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   194
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   195
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   196
(* equality and inclusion *)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   197
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   198
local
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   199
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   200
fun exists_ids (Theory ({id, ids, ...}, _, _, _)) (i, _) =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   201
  i = #1 id orelse is_some (Inttab.lookup (ids, i));
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   202
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   203
fun member_ids (Theory ({id, ...}, _, _, _), thy) = exists_ids thy id;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   204
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   205
fun subset_ids (Theory ({id, ids, ...}, _, _, _), thy) =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   206
  exists_ids thy id andalso Inttab.forall (exists_ids thy) ids;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   207
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   208
in
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   209
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   210
val eq_thy = eq_id o pairself (#id o identity_of o check_thy "Context.eq_thy");
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   211
fun subthy thys = eq_thy thys orelse member_ids thys;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   212
fun subthy_internal thys = eq_thy thys orelse subset_ids thys;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   213
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   214
end;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   215
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   216
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   217
(* external references *)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   218
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   219
datatype theory_ref = TheoryRef of theory ref;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   220
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   221
val self_ref = TheoryRef o the o #self o identity_of o check_thy "Context.self_ref";
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   222
fun deref (TheoryRef (ref thy)) = thy;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   223
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   224
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   225
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   226
(** theory data **)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   227
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   228
(* data kinds and access methods *)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   229
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   230
exception DATA_FAIL of exn * string;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   231
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   232
local
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   233
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   234
type kind =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   235
 {name: string,
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   236
  empty: Object.T,
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   237
  copy: Object.T -> Object.T,
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   238
  extend: Object.T -> Object.T,
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   239
  merge: Pretty.pp -> Object.T * Object.T -> Object.T,
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   240
  print: theory -> Object.T -> unit};
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   241
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   242
val kinds = ref (Inttab.empty: kind Inttab.table);
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   243
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   244
fun invoke meth_name meth_fn k =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   245
  (case Inttab.lookup (! kinds, k) of
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   246
    SOME kind => meth_fn kind |> transform_failure (fn exn =>
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   247
      DATA_FAIL (exn, "Theory data method " ^ #name kind ^ "." ^ meth_name ^ " failed"))
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   248
  | NONE => sys_error ("Invalid theory data identifier " ^ string_of_int k));
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   249
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   250
in
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   251
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   252
fun invoke_name k    = invoke "name" (K o #name) k ();
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   253
fun invoke_empty k   = invoke "empty" (K o #empty) k ();
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   254
val invoke_copy      = invoke "copy" #copy;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   255
val invoke_extend    = invoke "extend" #extend;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   256
fun invoke_merge pp  = invoke "merge" (fn kind => #merge kind pp);
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   257
fun invoke_print thy = invoke "print" (fn kind => #print kind thy);
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   258
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   259
fun declare name e cp ext mrg prt =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   260
  let
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   261
    val k = serial ();
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   262
    val kind = {name = name, empty = e, copy = cp, extend = ext, merge = mrg, print = prt};
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   263
    val _ = conditional (Inttab.exists (equal name o #name o #2) (! kinds)) (fn () =>
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   264
      warning ("Duplicate declaration of theory data " ^ quote name));
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   265
    val _ = kinds := Inttab.update ((k, kind), ! kinds);
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   266
  in k end;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   267
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   268
val copy_data = Inttab.map' invoke_copy;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   269
val extend_data = Inttab.map' invoke_extend;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   270
fun merge_data pp = Inttab.join (SOME oo invoke_merge pp) o pairself extend_data;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   271
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   272
fun theory_data thy =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   273
  map invoke_name (Inttab.keys (data_of thy))
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   274
  |> map (rpair ()) |> Symtab.make_multi |> Symtab.dest
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   275
  |> map (apsnd length)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   276
  |> map (fn (name, 1) => name | (name, n) => name ^ enclose "[" "]" (string_of_int n));
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   277
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   278
fun print_all_data thy =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   279
  List.app (uncurry (invoke_print thy)) (Inttab.dest (data_of thy));
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   280
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   281
end;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   282
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   283
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   284
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   285
(** build theories **)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   286
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   287
(* primitives *)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   288
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   289
fun create_thy name self id ids data history ancestry =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   290
  let
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   291
    val ids' = check_insert id ids;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   292
    val id' = (serial (), name);
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   293
    val _ = check_insert id' ids';
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   294
    val identity' = make_identity self id' ids';
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   295
  in vitalize (Theory (identity', data, history, ancestry)) end;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   296
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   297
fun copy_thy (thy as Theory ({id, ids, ...}, data, history, ancestry)) =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   298
  let val _ = check_thy "Context.copy_thy" thy;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   299
  in create_thy draftN NONE id ids (copy_data data) history ancestry end;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   300
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   301
fun change_thy name f (thy as Theory ({self, id, ids}, data, history, ancestry)) =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   302
  let
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   303
    val _ = check_thy "Context.change_thy" thy;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   304
    val (self', data', ancestry') =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   305
      if is_draft thy then (self, data, ancestry)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   306
      else (NONE, extend_data data, make_ancestry [thy] (thy :: #ancestors ancestry));
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   307
  in create_thy name self' id ids (f data') history ancestry' end;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   308
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   309
fun name_thy name = change_thy name I;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   310
val map_thy = change_thy draftN;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   311
val extend_thy = map_thy I;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   312
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   313
fun checkpoint_thy thy =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   314
  if not (is_draft thy) then thy
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   315
  else
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   316
    let
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   317
      val {name, version, intermediates} = history_of thy;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   318
      val thy' as Theory (identity', data', _, ancestry') =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   319
        name_thy (name ^ ":" ^ string_of_int version) thy;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   320
      val history' = make_history name (version + 1) (thy' :: intermediates);
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   321
    in vitalize (Theory (identity', data', history', ancestry')) end;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   322
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   323
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   324
(* theory data *)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   325
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   326
structure TheoryData =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   327
struct
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   328
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   329
val declare = declare;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   330
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   331
fun get k dest thy =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   332
  (case Inttab.lookup (data_of thy, k) of
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   333
    SOME x => (dest x handle Match =>
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   334
      error ("Failed to access theory data " ^ quote (invoke_name k)))
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   335
  | NONE => error ("Uninitialized theory data " ^ quote (invoke_name k)));
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   336
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   337
fun print k thy = invoke_print thy k (get k I thy);
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   338
fun put k mk x = map_thy (curry Inttab.update (k, mk x));
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   339
fun init k = put k I (invoke_empty k);
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   340
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   341
end;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   342
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   343
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   344
(* pre_pure *)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   345
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   346
val pre_pure = create_thy draftN NONE (serial (), draftN) Inttab.empty
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   347
  Inttab.empty (make_history ProtoPureN 0 []) (make_ancestry [] []);
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   348
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   349
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   350
(* trivial merge *)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   351
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   352
fun merge (thy1, thy2) =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   353
  if subthy (thy2, thy1) then thy1
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   354
  else if subthy (thy1, thy2) then thy2
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   355
  else (check_merge thy1 thy2;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   356
    raise TERM (cat_lines ["Attempt to perform non-trivial merge of theories:",
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   357
      str_of_thy thy1, str_of_thy thy2], []));
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   358
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   359
fun merge_refs (ref1, ref2) = self_ref (merge (deref ref1, deref ref2));
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   360
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   361
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   362
(* non-trivial merge *)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   363
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   364
fun merge_internal pp (thy1, thy2) =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   365
  if subthy_internal (thy2, thy1) then thy1
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   366
  else if subthy_internal (thy1, thy2) then thy2
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   367
  else if exists_name CPureN thy1 <> exists_name CPureN thy2 then
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   368
    error "Cannot merge Pure and CPure developments"
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   369
  else
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   370
    let
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   371
      val ids = check_merge thy1 thy2;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   372
      val data = merge_data (pp thy1) (data_of thy1, data_of thy2);
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   373
      val history = make_history "" 0 [];
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   374
      val ancestry = make_ancestry [] [];
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   375
    in create_thy draftN NONE (serial (), draftN) ids data history ancestry end;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   376
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   377
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   378
(* named theory nodes *)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   379
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   380
val theory_name = #name o history_of;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   381
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   382
fun begin_theory pp name imports =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   383
  if name = draftN then
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   384
    error ("Illegal theory name: " ^ quote draftN)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   385
  else if exists is_draft imports then
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   386
    error "Attempt to import draft theories"
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   387
  else
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   388
    let
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   389
      val parents = gen_distinct eq_thy imports;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   390
      val ancestors = gen_distinct eq_thy (parents @ List.concat (map ancestors_of parents));
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   391
      val Theory ({id, ids, ...}, data, _, _) =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   392
        (case parents of
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   393
          [] => error "No parent theories"
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   394
        | thy :: thys => extend_thy (Library.foldl (merge_internal pp) (thy, thys)));
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   395
      val history = make_history name 0 [];
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   396
      val ancestry = make_ancestry parents ancestors;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   397
    in create_thy draftN NONE id ids data history ancestry end;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   398
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   399
fun end_theory thy =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   400
  thy
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   401
(*|> purge_thy  FIXME *)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   402
  |> name_thy (theory_name thy);
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   403
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   404
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   405
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   406
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   407
(*** ML theory context ***)
6185
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
   408
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
   409
local
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   410
  val current_theory = ref (NONE: theory option);
6185
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
   411
in
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
   412
  fun get_context () = ! current_theory;
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
   413
  fun set_context opt_thy = current_theory := opt_thy;
6238
bd7b4a23118f setmp: theory option;
wenzelm
parents: 6185
diff changeset
   414
  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
   415
end;
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
   416
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
   417
fun the_context () =
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
   418
  (case get_context () of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   419
    SOME thy => thy
6185
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
   420
  | _ => error "Unknown theory context");
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
   421
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   422
fun context thy = set_context (SOME thy);
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   423
fun reset_context () = set_context NONE;
6185
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
   424
6310
wenzelm
parents: 6261
diff changeset
   425
fun pass opt_thy f x =
6261
6dc692fb3d28 added fetch, fetch_theory;
wenzelm
parents: 6238
diff changeset
   426
  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
   427
6310
wenzelm
parents: 6261
diff changeset
   428
fun pass_theory thy f x =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   429
  (case pass (SOME thy) f x of
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   430
    (y, SOME thy') => (y, thy')
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   431
  | (_, NONE) => error "Lost theory context in ML");
6261
6dc692fb3d28 added fetch, fetch_theory;
wenzelm
parents: 6238
diff changeset
   432
6238
bd7b4a23118f setmp: theory option;
wenzelm
parents: 6185
diff changeset
   433
fun save f x = setmp (get_context ()) f x;
bd7b4a23118f setmp: theory option;
wenzelm
parents: 6185
diff changeset
   434
6185
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
   435
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
   436
(* map context *)
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
   437
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
   438
nonfix >>;
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   439
fun >> f = set_context (SOME (f (the_context ())));
6185
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
   440
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
   441
8348
ebbbfdb35c84 added use_mltext, use_mltext_theory, use_let, use_setup (from isar_thy.ML);
wenzelm
parents: 6310
diff changeset
   442
(* use ML text *)
ebbbfdb35c84 added use_mltext, use_mltext_theory, use_let, use_setup (from isar_thy.ML);
wenzelm
parents: 6310
diff changeset
   443
16436
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   444
val ml_output = (writeln, error_msg);
14976
65f572245276 use_output: Symbol.escape;
wenzelm
parents: 11819
diff changeset
   445
65f572245276 use_output: Symbol.escape;
wenzelm
parents: 11819
diff changeset
   446
fun use_output verb txt = use_text ml_output verb (Symbol.escape txt);
10914
aded4ba99b88 use_text etc.: proper output of error messages;
wenzelm
parents: 10893
diff changeset
   447
10924
92305ae9f460 use_output: proper handling of non-ASCII symbols;
wenzelm
parents: 10914
diff changeset
   448
fun use_mltext txt verb opt_thy = setmp opt_thy (fn () => use_output verb txt) ();
92305ae9f460 use_output: proper handling of non-ASCII symbols;
wenzelm
parents: 10914
diff changeset
   449
fun use_mltext_theory txt verb thy = #2 (pass_theory thy (use_output verb) txt);
8348
ebbbfdb35c84 added use_mltext, use_mltext_theory, use_let, use_setup (from isar_thy.ML);
wenzelm
parents: 6310
diff changeset
   450
ebbbfdb35c84 added use_mltext, use_mltext_theory, use_let, use_setup (from isar_thy.ML);
wenzelm
parents: 6310
diff changeset
   451
fun use_context txt = use_mltext_theory ("Context.>> (" ^ txt ^ ");") false;
ebbbfdb35c84 added use_mltext, use_mltext_theory, use_let, use_setup (from isar_thy.ML);
wenzelm
parents: 6310
diff changeset
   452
9586
f6669dead969 use_let: exclude 'val';
wenzelm
parents: 8801
diff changeset
   453
fun use_let bind body txt =
f6669dead969 use_let: exclude 'val';
wenzelm
parents: 8801
diff changeset
   454
  use_context ("let " ^ bind ^ " = " ^ txt ^ " in\n" ^ body ^ " end");
8348
ebbbfdb35c84 added use_mltext, use_mltext_theory, use_let, use_setup (from isar_thy.ML);
wenzelm
parents: 6310
diff changeset
   455
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15531
diff changeset
   456
16436
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   457
(* delayed theory setup *)
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15531
diff changeset
   458
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15531
diff changeset
   459
local
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15531
diff changeset
   460
  val setup_fns = ref ([]: (theory -> theory) list);
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15531
diff changeset
   461
in
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15531
diff changeset
   462
  fun add_setup fns = setup_fns := ! setup_fns @ fns;
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15531
diff changeset
   463
  fun setup () = let val fns = ! setup_fns in setup_fns := []; fns end;
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15531
diff changeset
   464
end;
8348
ebbbfdb35c84 added use_mltext, use_mltext_theory, use_let, use_setup (from isar_thy.ML);
wenzelm
parents: 6310
diff changeset
   465
6185
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
   466
end;
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
   467
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
   468
structure BasicContext: BASIC_CONTEXT = Context;
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
   469
open BasicContext;
16436
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   470
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   471
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   472
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   473
(*** type-safe interface for theory data ***)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   474
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   475
signature THEORY_DATA_ARGS =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   476
sig
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   477
  val name: string
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   478
  type T
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   479
  val empty: T
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   480
  val copy: T -> T
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   481
  val extend: T -> T
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   482
  val merge: Pretty.pp -> T * T -> T
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   483
  val print: theory -> T -> unit
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   484
end;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   485
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   486
signature THEORY_DATA =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   487
sig
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   488
  type T
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   489
  val init: theory -> theory
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   490
  val print: theory -> unit
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   491
  val get: theory -> T
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   492
  val put: T -> theory -> theory
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   493
  val map: (T -> T) -> theory -> theory
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   494
end;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   495
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   496
functor TheoryDataFun(Data: THEORY_DATA_ARGS): THEORY_DATA =
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   497
struct
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   498
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   499
structure TheoryData = Context.TheoryData;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   500
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   501
type T = Data.T;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   502
exception Data of T;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   503
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   504
val kind = TheoryData.declare Data.name
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   505
  (Data Data.empty)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   506
  (fn Data x => Data (Data.copy x))
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   507
  (fn Data x => Data (Data.extend x))
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   508
  (fn pp => fn (Data x1, Data x2) => Data (Data.merge pp (x1, x2)))
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   509
  (fn thy => fn Data x => Data.print thy x);
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   510
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   511
val init = TheoryData.init kind;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   512
val print = TheoryData.print kind;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   513
val get = TheoryData.get kind (fn Data x => x);
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   514
val get_sg = get;    (*obsolete*)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   515
val put = TheoryData.put kind Data;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   516
fun map f thy = put (f (get thy)) thy;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   517
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   518
end;
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   519
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   520
(*hide private interface!*)
7eb6b6cbd166 added type theory: generic theory contexts with unique identity,
wenzelm
parents: 15801
diff changeset
   521
structure Context: CONTEXT = Context;