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