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