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