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