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