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