src/Pure/context.ML
changeset 59163 857a600f0c94
parent 59146 ba2a326fc271
child 60947 d5f7b424ba47
equal deleted inserted replaced
59162:dca5594761f2 59163:857a600f0c94
   363 structure Proof =
   363 structure Proof =
   364 struct
   364 struct
   365   datatype context = Context of Any.T Datatab.table * theory;
   365   datatype context = Context of Any.T Datatab.table * theory;
   366 end;
   366 end;
   367 
   367 
   368 fun theory_of_proof (Proof.Context (_, thy)) = thy;
       
   369 fun data_of_proof (Proof.Context (data, _)) = data;
       
   370 fun map_prf f (Proof.Context (data, thy)) = Proof.Context (f data, thy);
       
   371 
       
   372 
   368 
   373 (* proof data kinds *)
   369 (* proof data kinds *)
   374 
   370 
   375 local
   371 local
   376 
   372 
   377 val kinds = Synchronized.var "Proof_Data" (Datatab.empty: (theory -> Any.T) Datatab.table);
   373 val kinds = Synchronized.var "Proof_Data" (Datatab.empty: (theory -> Any.T) Datatab.table);
   378 
   374 
   379 fun invoke_init tab k =
   375 fun init_data thy =
   380   (case Datatab.lookup tab k of
   376   Synchronized.value kinds |> Datatab.map (fn _ => fn init => init thy);
   381     SOME init => init
   377 
       
   378 fun init_new_data thy =
       
   379   Synchronized.value kinds |> Datatab.fold (fn (k, init) => fn data =>
       
   380     if Datatab.defined data k then data
       
   381     else Datatab.update (k, init thy) data);
       
   382 
       
   383 fun init_fallback k thy =
       
   384   (case Datatab.lookup (Synchronized.value kinds) k of
       
   385     SOME init => init thy
   382   | NONE => raise Fail "Invalid proof data identifier");
   386   | NONE => raise Fail "Invalid proof data identifier");
   383 
   387 
   384 fun init_data thy =
       
   385   let val tab = Synchronized.value kinds
       
   386   in Datatab.map (fn k => fn _ => invoke_init tab k thy) tab end;
       
   387 
       
   388 fun init_new_data data thy =
       
   389   Datatab.merge (K true) (data, init_data thy);
       
   390 
       
   391 in
   388 in
   392 
   389 
   393 fun raw_transfer thy' (Proof.Context (data, thy)) =
   390 fun raw_transfer thy' (Proof.Context (data, thy)) =
   394   let
   391   let
   395     val _ = subthy (thy, thy') orelse error "Cannot transfer proof context: not a super theory";
   392     val _ = subthy (thy, thy') orelse error "Cannot transfer proof context: not a super theory";
   396     val data' = init_new_data data thy';
   393     val data' = init_new_data thy' data;
   397   in Proof.Context (data', thy') end;
   394   in Proof.Context (data', thy') end;
   398 
   395 
   399 structure Proof_Context =
   396 structure Proof_Context =
   400 struct
   397 struct
   401   val theory_of = theory_of_proof;
   398   fun theory_of (Proof.Context (_, thy)) = thy;
   402   fun init_global thy = Proof.Context (init_data thy, thy);
   399   fun init_global thy = Proof.Context (init_data thy, thy);
   403   fun get_global thy name = init_global (get_theory thy name);
   400   fun get_global thy name = init_global (get_theory thy name);
   404 end;
   401 end;
   405 
   402 
   406 structure Proof_Data =
   403 structure Proof_Data =
   410   let
   407   let
   411     val k = serial ();
   408     val k = serial ();
   412     val _ = Synchronized.change kinds (Datatab.update (k, init));
   409     val _ = Synchronized.change kinds (Datatab.update (k, init));
   413   in k end;
   410   in k end;
   414 
   411 
   415 fun get k dest prf =
   412 fun get k dest (Proof.Context (data, thy)) =
   416   (case Datatab.lookup (data_of_proof prf) k of
   413   (case Datatab.lookup data k of
   417     SOME x => x
   414     SOME x => x
   418   | NONE =>
   415   | NONE => init_fallback k thy) |> dest;
   419       (*adhoc value for old theories*)
   416 
   420       invoke_init (Synchronized.value kinds) k (Proof_Context.theory_of prf))
   417 fun put k mk x (Proof.Context (data, thy)) =
   421   |> dest;
   418   Proof.Context (Datatab.update (k, mk x) data, thy);
   422 
       
   423 fun put k mk x = map_prf (Datatab.update (k, mk x));
       
   424 
   419 
   425 end;
   420 end;
   426 
   421 
   427 end;
   422 end;
   428 
   423