src/Pure/context.ML
author wenzelm
Sat Nov 04 15:24:40 2017 +0100 (19 months ago)
changeset 67003 49850a679c2c
parent 66452 450cefec7c11
child 67621 8f93d878f855
permissions -rw-r--r--
more robust sorted_entries;
     1 (*  Title:      Pure/context.ML
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     4 Generic theory contexts with unique identity, arbitrarily typed data,
     5 monotonic development graph and history support.  Generic proof
     6 contexts with arbitrarily typed data.
     7 
     8 Firm naming conventions:
     9    thy, thy', thy1, thy2: theory
    10    ctxt, ctxt', ctxt1, ctxt2: Proof.context
    11    context: Context.generic
    12 *)
    13 
    14 signature BASIC_CONTEXT =
    15 sig
    16   type theory
    17   exception THEORY of string * theory list
    18   structure Proof: sig type context end
    19   structure Proof_Context:
    20   sig
    21     val theory_of: Proof.context -> theory
    22     val init_global: theory -> Proof.context
    23     val get_global: theory -> string -> Proof.context
    24   end
    25 end;
    26 
    27 signature CONTEXT =
    28 sig
    29   include BASIC_CONTEXT
    30   (*theory context*)
    31   type theory_id
    32   val theory_id: theory -> theory_id
    33   val timing: bool Unsynchronized.ref
    34   val parents_of: theory -> theory list
    35   val ancestors_of: theory -> theory list
    36   val theory_id_long_name: theory_id -> string
    37   val theory_id_name: theory_id -> string
    38   val theory_long_name: theory -> string
    39   val theory_name: theory -> string
    40   val PureN: string
    41   val pretty_thy: theory -> Pretty.T
    42   val pretty_abbrev_thy: theory -> Pretty.T
    43   val get_theory: theory -> string -> theory
    44   val this_theory: theory -> string -> theory
    45   val eq_thy_id: theory_id * theory_id -> bool
    46   val eq_thy: theory * theory -> bool
    47   val proper_subthy_id: theory_id * theory_id -> bool
    48   val proper_subthy: theory * theory -> bool
    49   val subthy_id: theory_id * theory_id -> bool
    50   val subthy: theory * theory -> bool
    51   val finish_thy: theory -> theory
    52   val begin_thy: string -> theory list -> theory
    53   (*proof context*)
    54   val raw_transfer: theory -> Proof.context -> Proof.context
    55   (*certificate*)
    56   datatype certificate = Certificate of theory | Certificate_Id of theory_id
    57   val certificate_theory: certificate -> theory
    58   val certificate_theory_id: certificate -> theory_id
    59   val eq_certificate: certificate * certificate -> bool
    60   val join_certificate: certificate * certificate -> certificate
    61   (*generic context*)
    62   datatype generic = Theory of theory | Proof of Proof.context
    63   val cases: (theory -> 'a) -> (Proof.context -> 'a) -> generic -> 'a
    64   val mapping: (theory -> theory) -> (Proof.context -> Proof.context) -> generic -> generic
    65   val mapping_result: (theory -> 'a * theory) -> (Proof.context -> 'a * Proof.context) ->
    66     generic -> 'a * generic
    67   val the_theory: generic -> theory
    68   val the_proof: generic -> Proof.context
    69   val map_theory: (theory -> theory) -> generic -> generic
    70   val map_proof: (Proof.context -> Proof.context) -> generic -> generic
    71   val map_theory_result: (theory -> 'a * theory) -> generic -> 'a * generic
    72   val map_proof_result: (Proof.context -> 'a * Proof.context) -> generic -> 'a * generic
    73   val theory_map: (generic -> generic) -> theory -> theory
    74   val proof_map: (generic -> generic) -> Proof.context -> Proof.context
    75   val theory_of: generic -> theory  (*total*)
    76   val proof_of: generic -> Proof.context  (*total*)
    77   (*thread data*)
    78   val get_generic_context: unit -> generic option
    79   val put_generic_context: generic option -> unit
    80   val setmp_generic_context: generic option -> ('a -> 'b) -> 'a -> 'b
    81   val the_generic_context: unit -> generic
    82   val the_global_context: unit -> theory
    83   val the_local_context: unit -> Proof.context
    84   val >> : (generic -> generic) -> unit
    85   val >>> : (generic -> 'a * generic) -> 'a
    86 end;
    87 
    88 signature PRIVATE_CONTEXT =
    89 sig
    90   include CONTEXT
    91   structure Theory_Data:
    92   sig
    93     val declare: Position.T -> Any.T -> (Any.T -> Any.T) ->
    94       (theory * theory -> Any.T * Any.T -> Any.T) -> serial
    95     val get: serial -> (Any.T -> 'a) -> theory -> 'a
    96     val put: serial -> ('a -> Any.T) -> 'a -> theory -> theory
    97   end
    98   structure Proof_Data:
    99   sig
   100     val declare: (theory -> Any.T) -> serial
   101     val get: serial -> (Any.T -> 'a) -> Proof.context -> 'a
   102     val put: serial -> ('a -> Any.T) -> 'a -> Proof.context -> Proof.context
   103   end
   104 end;
   105 
   106 structure Context: PRIVATE_CONTEXT =
   107 struct
   108 
   109 (*** theory context ***)
   110 
   111 (*private copy avoids potential conflict of table exceptions*)
   112 structure Datatab = Table(type key = int val ord = int_ord);
   113 
   114 
   115 (** datatype theory **)
   116 
   117 datatype theory_id =
   118   Theory_Id of
   119    (*identity*)
   120    {id: serial,                   (*identifier*)
   121     ids: Inttab.set} *            (*cumulative identifiers -- symbolic body content*)
   122    (*history*)
   123    {name: string,                 (*official theory name*)
   124     stage: int};                  (*counter for anonymous updates*)
   125 
   126 datatype theory =
   127   Theory of
   128    theory_id *
   129    (*ancestry*)
   130    {parents: theory list,         (*immediate predecessors*)
   131     ancestors: theory list} *     (*all predecessors -- canonical reverse order*)
   132    (*data*)
   133    Any.T Datatab.table;           (*body content*)
   134 
   135 exception THEORY of string * theory list;
   136 
   137 fun rep_theory_id (Theory_Id args) = args;
   138 fun rep_theory (Theory args) = args;
   139 
   140 val theory_id = #1 o rep_theory;
   141 
   142 val identity_of_id = #1 o rep_theory_id;
   143 val identity_of = identity_of_id o theory_id;
   144 val history_of_id = #2 o rep_theory_id;
   145 val history_of = history_of_id o theory_id;
   146 val ancestry_of = #2 o rep_theory;
   147 val data_of = #3 o rep_theory;
   148 
   149 fun make_identity id ids = {id = id, ids = ids};
   150 fun make_history name stage = {name = name, stage = stage};
   151 fun make_ancestry parents ancestors = {parents = parents, ancestors = ancestors};
   152 
   153 val theory_id_long_name = #name o history_of_id;
   154 val theory_id_name = Long_Name.base_name o theory_id_long_name;
   155 val theory_long_name = #name o history_of;
   156 val theory_name = Long_Name.base_name o theory_long_name;
   157 
   158 val parents_of = #parents o ancestry_of;
   159 val ancestors_of = #ancestors o ancestry_of;
   160 
   161 
   162 (* names *)
   163 
   164 val PureN = "Pure";
   165 val finished = ~1;
   166 
   167 fun display_name thy_id =
   168   let val {name, stage} = history_of_id thy_id;
   169   in if stage = finished then name else name ^ ":" ^ string_of_int stage end;
   170 
   171 fun display_names thy =
   172   let
   173     val name = display_name (theory_id thy);
   174     val ancestor_names = map theory_name (ancestors_of thy);
   175   in rev (name :: ancestor_names) end;
   176 
   177 val pretty_thy = Pretty.str_list "{" "}" o display_names;
   178 
   179 val _ = ML_system_pp (fn _ => fn _ => Pretty.to_polyml o pretty_thy);
   180 
   181 fun pretty_abbrev_thy thy =
   182   let
   183     val names = display_names thy;
   184     val n = length names;
   185     val abbrev = if n > 5 then "..." :: List.drop (names, n - 5) else names;
   186   in Pretty.str_list "{" "}" abbrev end;
   187 
   188 fun get_theory thy name =
   189   if theory_name thy <> name then
   190     (case find_first (fn thy' => theory_name thy' = name) (ancestors_of thy) of
   191       SOME thy' => thy'
   192     | NONE => error ("Unknown ancestor theory " ^ quote name))
   193   else if #stage (history_of thy) = finished then thy
   194   else error ("Unfinished theory " ^ quote name);
   195 
   196 fun this_theory thy name =
   197   if theory_name thy = name then thy
   198   else get_theory thy name;
   199 
   200 
   201 (* build ids *)
   202 
   203 fun insert_id id ids = Inttab.update (id, ()) ids;
   204 
   205 fun merge_ids
   206     (Theory (Theory_Id ({id = id1, ids = ids1, ...}, _), _, _))
   207     (Theory (Theory_Id ({id = id2, ids = ids2, ...}, _), _, _)) =
   208   Inttab.merge (K true) (ids1, ids2)
   209   |> insert_id id1
   210   |> insert_id id2;
   211 
   212 
   213 (* equality and inclusion *)
   214 
   215 val eq_thy_id = op = o apply2 (#id o identity_of_id);
   216 val eq_thy = op = o apply2 (#id o identity_of);
   217 
   218 fun proper_subthy_id (Theory_Id ({id, ...}, _), Theory_Id ({ids, ...}, _)) = Inttab.defined ids id;
   219 val proper_subthy = proper_subthy_id o apply2 theory_id;
   220 
   221 fun subthy_id p = eq_thy_id p orelse proper_subthy_id p;
   222 val subthy = subthy_id o apply2 theory_id;
   223 
   224 
   225 (* consistent ancestors *)
   226 
   227 fun eq_thy_consistent (thy1, thy2) =
   228   eq_thy (thy1, thy2) orelse
   229     (theory_name thy1 = theory_name thy2 andalso
   230       raise THEORY ("Duplicate theory name", [thy1, thy2]));
   231 
   232 fun extend_ancestors thy thys =
   233   if member eq_thy_consistent thys thy then
   234     raise THEORY ("Duplicate theory node", thy :: thys)
   235   else thy :: thys;
   236 
   237 val merge_ancestors = merge eq_thy_consistent;
   238 
   239 
   240 
   241 (** theory data **)
   242 
   243 (* data kinds and access methods *)
   244 
   245 val timing = Unsynchronized.ref false;
   246 
   247 local
   248 
   249 type kind =
   250  {pos: Position.T,
   251   empty: Any.T,
   252   extend: Any.T -> Any.T,
   253   merge: theory * theory -> Any.T * Any.T -> Any.T};
   254 
   255 val kinds = Synchronized.var "Theory_Data" (Datatab.empty: kind Datatab.table);
   256 
   257 fun invoke name f k x =
   258   (case Datatab.lookup (Synchronized.value kinds) k of
   259     SOME kind =>
   260       if ! timing andalso name <> "" then
   261         Timing.cond_timeit true ("Theory_Data." ^ name ^ Position.here (#pos kind))
   262           (fn () => f kind x)
   263       else f kind x
   264   | NONE => raise Fail "Invalid theory data identifier");
   265 
   266 in
   267 
   268 fun invoke_empty k = invoke "" (K o #empty) k ();
   269 val invoke_extend = invoke "extend" #extend;
   270 fun invoke_merge thys = invoke "merge" (fn kind => #merge kind thys);
   271 
   272 fun declare_theory_data pos empty extend merge =
   273   let
   274     val k = serial ();
   275     val kind = {pos = pos, empty = empty, extend = extend, merge = merge};
   276     val _ = Synchronized.change kinds (Datatab.update (k, kind));
   277   in k end;
   278 
   279 val extend_data = Datatab.map invoke_extend;
   280 fun merge_data thys = Datatab.join (invoke_merge thys) o apply2 extend_data;
   281 
   282 end;
   283 
   284 
   285 
   286 (** build theories **)
   287 
   288 (* primitives *)
   289 
   290 fun create_thy ids history ancestry data =
   291   Theory (Theory_Id (make_identity (serial ()) ids, history), ancestry, data);
   292 
   293 val pre_pure_thy =
   294   create_thy Inttab.empty (make_history PureN 0) (make_ancestry [] []) Datatab.empty;
   295 
   296 local
   297 
   298 fun change_thy finish f thy =
   299   let
   300     val Theory (Theory_Id ({id, ids}, {name, stage}), ancestry, data) = thy;
   301     val (ancestry', data') =
   302       if stage = finished then
   303         (make_ancestry [thy] (extend_ancestors thy (ancestors_of thy)), extend_data data)
   304       else (ancestry, data);
   305     val history' = {name = name, stage = if finish then finished else stage + 1};
   306     val ids' = insert_id id ids;
   307     val data'' = f data';
   308   in create_thy ids' history' ancestry' data'' end;
   309 
   310 in
   311 
   312 val update_thy = change_thy false;
   313 val extend_thy = update_thy I;
   314 val finish_thy = change_thy true I;
   315 
   316 end;
   317 
   318 
   319 (* named theory nodes *)
   320 
   321 local
   322 
   323 fun merge_thys (thy1, thy2) =
   324   let
   325     val ids = merge_ids thy1 thy2;
   326     val history = make_history "" 0;
   327     val ancestry = make_ancestry [] [];
   328     val data = merge_data (thy1, thy2) (data_of thy1, data_of thy2);
   329   in create_thy ids history ancestry data end;
   330 
   331 fun maximal_thys thys =
   332   thys |> filter_out (fn thy => exists (fn thy' => proper_subthy (thy, thy')) thys);
   333 
   334 in
   335 
   336 fun begin_thy name imports =
   337   if name = "" then error ("Bad theory name: " ^ quote name)
   338   else
   339     let
   340       val parents = maximal_thys (distinct eq_thy imports);
   341       val ancestors =
   342         Library.foldl merge_ancestors ([], map ancestors_of parents)
   343         |> fold extend_ancestors parents;
   344 
   345       val Theory (Theory_Id ({ids, ...}, _), _, data) =
   346         (case parents of
   347           [] => error "Missing theory imports"
   348         | [thy] => extend_thy thy
   349         | thy :: thys => Library.foldl merge_thys (thy, thys));
   350 
   351       val history = make_history name 0;
   352       val ancestry = make_ancestry parents ancestors;
   353     in create_thy ids history ancestry data end;
   354 
   355 end;
   356 
   357 
   358 (* theory data *)
   359 
   360 structure Theory_Data =
   361 struct
   362 
   363 val declare = declare_theory_data;
   364 
   365 fun get k dest thy =
   366   (case Datatab.lookup (data_of thy) k of
   367     SOME x => x
   368   | NONE => invoke_empty k) |> dest;
   369 
   370 fun put k mk x = update_thy (Datatab.update (k, mk x));
   371 
   372 end;
   373 
   374 
   375 
   376 (*** proof context ***)
   377 
   378 (* datatype Proof.context *)
   379 
   380 structure Proof =
   381 struct
   382   datatype context = Context of Any.T Datatab.table * theory;
   383 end;
   384 
   385 
   386 (* proof data kinds *)
   387 
   388 local
   389 
   390 val kinds = Synchronized.var "Proof_Data" (Datatab.empty: (theory -> Any.T) Datatab.table);
   391 
   392 fun init_data thy =
   393   Synchronized.value kinds |> Datatab.map (fn _ => fn init => init thy);
   394 
   395 fun init_new_data thy =
   396   Synchronized.value kinds |> Datatab.fold (fn (k, init) => fn data =>
   397     if Datatab.defined data k then data
   398     else Datatab.update (k, init thy) data);
   399 
   400 fun init_fallback k thy =
   401   (case Datatab.lookup (Synchronized.value kinds) k of
   402     SOME init => init thy
   403   | NONE => raise Fail "Invalid proof data identifier");
   404 
   405 in
   406 
   407 fun raw_transfer thy' (Proof.Context (data, thy)) =
   408   let
   409     val _ = subthy (thy, thy') orelse error "Cannot transfer proof context: not a super theory";
   410     val data' = init_new_data thy' data;
   411   in Proof.Context (data', thy') end;
   412 
   413 structure Proof_Context =
   414 struct
   415   fun theory_of (Proof.Context (_, thy)) = thy;
   416   fun init_global thy = Proof.Context (init_data thy, thy);
   417   fun get_global thy name = init_global (get_theory thy name);
   418 end;
   419 
   420 structure Proof_Data =
   421 struct
   422 
   423 fun declare init =
   424   let
   425     val k = serial ();
   426     val _ = Synchronized.change kinds (Datatab.update (k, init));
   427   in k end;
   428 
   429 fun get k dest (Proof.Context (data, thy)) =
   430   (case Datatab.lookup data k of
   431     SOME x => x
   432   | NONE => init_fallback k thy) |> dest;
   433 
   434 fun put k mk x (Proof.Context (data, thy)) =
   435   Proof.Context (Datatab.update (k, mk x) data, thy);
   436 
   437 end;
   438 
   439 end;
   440 
   441 
   442 
   443 (*** theory certificate ***)
   444 
   445 datatype certificate = Certificate of theory | Certificate_Id of theory_id;
   446 
   447 fun certificate_theory (Certificate thy) = thy
   448   | certificate_theory (Certificate_Id thy_id) =
   449       error ("No content for theory certificate " ^ display_name thy_id);
   450 
   451 fun certificate_theory_id (Certificate thy) = theory_id thy
   452   | certificate_theory_id (Certificate_Id thy_id) = thy_id;
   453 
   454 fun eq_certificate (Certificate thy1, Certificate thy2) = eq_thy (thy1, thy2)
   455   | eq_certificate (Certificate_Id thy_id1, Certificate_Id thy_id2) = eq_thy_id (thy_id1, thy_id2)
   456   | eq_certificate _ = false;
   457 
   458 fun join_certificate (cert1, cert2) =
   459   let val (thy_id1, thy_id2) = apply2 certificate_theory_id (cert1, cert2) in
   460     if eq_thy_id (thy_id1, thy_id2) then (case cert1 of Certificate _ => cert1 | _ => cert2)
   461     else if proper_subthy_id (thy_id2, thy_id1) then cert1
   462     else if proper_subthy_id (thy_id1, thy_id2) then cert2
   463     else
   464       error ("Cannot join unrelated theory certificates " ^
   465         display_name thy_id1 ^ " and " ^ display_name thy_id2)
   466   end;
   467 
   468 
   469 
   470 (*** generic context ***)
   471 
   472 datatype generic = Theory of theory | Proof of Proof.context;
   473 
   474 fun cases f _ (Theory thy) = f thy
   475   | cases _ g (Proof prf) = g prf;
   476 
   477 fun mapping f g = cases (Theory o f) (Proof o g);
   478 fun mapping_result f g = cases (apsnd Theory o f) (apsnd Proof o g);
   479 
   480 val the_theory = cases I (fn _ => error "Ill-typed context: theory expected");
   481 val the_proof = cases (fn _ => error "Ill-typed context: proof expected") I;
   482 
   483 fun map_theory f = Theory o f o the_theory;
   484 fun map_proof f = Proof o f o the_proof;
   485 
   486 fun map_theory_result f = apsnd Theory o f o the_theory;
   487 fun map_proof_result f = apsnd Proof o f o the_proof;
   488 
   489 fun theory_map f = the_theory o f o Theory;
   490 fun proof_map f = the_proof o f o Proof;
   491 
   492 val theory_of = cases I Proof_Context.theory_of;
   493 val proof_of = cases Proof_Context.init_global I;
   494 
   495 
   496 
   497 (** thread data **)
   498 
   499 local val generic_context_var = Thread_Data.var () : generic Thread_Data.var in
   500 
   501 fun get_generic_context () = Thread_Data.get generic_context_var;
   502 val put_generic_context = Thread_Data.put generic_context_var;
   503 fun setmp_generic_context opt_context = Thread_Data.setmp generic_context_var opt_context;
   504 
   505 fun the_generic_context () =
   506   (case get_generic_context () of
   507     SOME context => context
   508   | _ => error "Unknown context");
   509 
   510 val the_global_context = theory_of o the_generic_context;
   511 val the_local_context = proof_of o the_generic_context;
   512 
   513 end;
   514 
   515 fun >>> f =
   516   let
   517     val (res, context') = f (the_generic_context ());
   518     val _ = put_generic_context (SOME context');
   519   in res end;
   520 
   521 nonfix >>;
   522 fun >> f = >>> (fn context => ((), f context));
   523 
   524 val _ = put_generic_context (SOME (Theory pre_pure_thy));
   525 
   526 end;
   527 
   528 structure Basic_Context: BASIC_CONTEXT = Context;
   529 open Basic_Context;
   530 
   531 
   532 
   533 (*** type-safe interfaces for data declarations ***)
   534 
   535 (** theory data **)
   536 
   537 signature THEORY_DATA'_ARGS =
   538 sig
   539   type T
   540   val empty: T
   541   val extend: T -> T
   542   val merge: theory * theory -> T * T -> T
   543 end;
   544 
   545 signature THEORY_DATA_ARGS =
   546 sig
   547   type T
   548   val empty: T
   549   val extend: T -> T
   550   val merge: T * T -> T
   551 end;
   552 
   553 signature THEORY_DATA =
   554 sig
   555   type T
   556   val get: theory -> T
   557   val put: T -> theory -> theory
   558   val map: (T -> T) -> theory -> theory
   559 end;
   560 
   561 functor Theory_Data'(Data: THEORY_DATA'_ARGS): THEORY_DATA =
   562 struct
   563 
   564 type T = Data.T;
   565 exception Data of T;
   566 
   567 val kind =
   568   Context.Theory_Data.declare
   569     (Position.thread_data ())
   570     (Data Data.empty)
   571     (fn Data x => Data (Data.extend x))
   572     (fn thys => fn (Data x1, Data x2) => Data (Data.merge thys (x1, x2)));
   573 
   574 val get = Context.Theory_Data.get kind (fn Data x => x);
   575 val put = Context.Theory_Data.put kind Data;
   576 fun map f thy = put (f (get thy)) thy;
   577 
   578 end;
   579 
   580 functor Theory_Data(Data: THEORY_DATA_ARGS): THEORY_DATA =
   581   Theory_Data'
   582   (
   583     type T = Data.T;
   584     val empty = Data.empty;
   585     val extend = Data.extend;
   586     fun merge _ = Data.merge;
   587   );
   588 
   589 
   590 
   591 (** proof data **)
   592 
   593 signature PROOF_DATA_ARGS =
   594 sig
   595   type T
   596   val init: theory -> T
   597 end;
   598 
   599 signature PROOF_DATA =
   600 sig
   601   type T
   602   val get: Proof.context -> T
   603   val put: T -> Proof.context -> Proof.context
   604   val map: (T -> T) -> Proof.context -> Proof.context
   605 end;
   606 
   607 functor Proof_Data(Data: PROOF_DATA_ARGS): PROOF_DATA =
   608 struct
   609 
   610 type T = Data.T;
   611 exception Data of T;
   612 
   613 val kind = Context.Proof_Data.declare (Data o Data.init);
   614 
   615 val get = Context.Proof_Data.get kind (fn Data x => x);
   616 val put = Context.Proof_Data.put kind Data;
   617 fun map f prf = put (f (get prf)) prf;
   618 
   619 end;
   620 
   621 
   622 
   623 (** generic data **)
   624 
   625 signature GENERIC_DATA_ARGS =
   626 sig
   627   type T
   628   val empty: T
   629   val extend: T -> T
   630   val merge: T * T -> T
   631 end;
   632 
   633 signature GENERIC_DATA =
   634 sig
   635   type T
   636   val get: Context.generic -> T
   637   val put: T -> Context.generic -> Context.generic
   638   val map: (T -> T) -> Context.generic -> Context.generic
   639 end;
   640 
   641 functor Generic_Data(Data: GENERIC_DATA_ARGS): GENERIC_DATA =
   642 struct
   643 
   644 structure Thy_Data = Theory_Data(Data);
   645 structure Prf_Data = Proof_Data(type T = Data.T val init = Thy_Data.get);
   646 
   647 type T = Data.T;
   648 
   649 fun get (Context.Theory thy) = Thy_Data.get thy
   650   | get (Context.Proof prf) = Prf_Data.get prf;
   651 
   652 fun put x (Context.Theory thy) = Context.Theory (Thy_Data.put x thy)
   653   | put x (Context.Proof prf) = Context.Proof (Prf_Data.put x prf);
   654 
   655 fun map f ctxt = put (f (get ctxt)) ctxt;
   656 
   657 end;
   658 
   659 (*hide private interface*)
   660 structure Context: CONTEXT = Context;