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