src/HOL/Tools/Ctr_Sugar/local_interpretation.ML
changeset 58177 166131276380
child 58178 695ba3101b37
equal deleted inserted replaced
58176:710710a66173 58177:166131276380
       
     1 (*  Title:      HOL/Tools/Ctr_Sugar/local_interpretation.ML
       
     2     Author:     Jasmin Blanchette, TU Muenchen
       
     3 
       
     4 Generic interpretation of local theory data -- ad hoc. Based on
       
     5 
       
     6     Title:      Pure/interpretation.ML
       
     7     Author:     Florian Haftmann and Makarius
       
     8 *)
       
     9 
       
    10 signature LOCAL_INTERPRETATION =
       
    11 sig
       
    12   type T
       
    13   val result: theory -> T list
       
    14   val interpretation: (T -> theory -> theory) -> (T -> local_theory -> local_theory) -> theory ->
       
    15     theory
       
    16   val data: T -> local_theory -> local_theory
       
    17   val data_global: T -> theory -> theory
       
    18   val init: theory -> theory
       
    19 end;
       
    20 
       
    21 functor Local_Interpretation(type T val eq: T * T -> bool): LOCAL_INTERPRETATION =
       
    22 struct
       
    23 
       
    24 type T = T;
       
    25 
       
    26 structure Interp = Theory_Data
       
    27 (
       
    28   type T =
       
    29     T list
       
    30     * ((((T -> theory -> theory) * (T -> local_theory -> local_theory)) * stamp) * T list) list;
       
    31   val empty = ([], []);
       
    32   val extend = I;
       
    33   fun merge ((data1, interps1), (data2, interps2)) : T =
       
    34     (Library.merge eq (data1, data2),
       
    35      AList.join (eq_snd (op =)) (K (Library.merge eq)) (interps1, interps2));
       
    36 );
       
    37 
       
    38 val result = #1 o Interp.get;
       
    39 
       
    40 fun consolidate lthy =
       
    41   let
       
    42     val thy = Proof_Context.theory_of lthy;
       
    43     val (data, interps) = Interp.get thy;
       
    44     val unfinished = interps |> map (fn (((_, f), _), xs) =>
       
    45       (f, if eq_list eq (xs, data) then [] else subtract eq xs data));
       
    46     val finished = interps |> map (fn (interp, _) => (interp, data));
       
    47   in
       
    48     if forall (null o #2) unfinished then NONE
       
    49     else SOME (lthy |> fold_rev (uncurry fold_rev) unfinished
       
    50       |> Local_Theory.background_theory (Interp.put (data, finished)))
       
    51   end;
       
    52 
       
    53 fun consolidate_global thy =
       
    54   let
       
    55     val (data, interps) = Interp.get thy;
       
    56     val unfinished = interps |> map (fn (((g, _), _), xs) =>
       
    57       (g, if eq_list eq (xs, data) then [] else subtract eq xs data));
       
    58     val finished = interps |> map (fn (interp, _) => (interp, data));
       
    59   in
       
    60     if forall (null o #2) unfinished then NONE
       
    61     else SOME (thy |> fold_rev (uncurry fold_rev) unfinished |> Interp.put (data, finished))
       
    62   end;
       
    63 
       
    64 fun interpretation g f =
       
    65   Interp.map (apsnd (cons (((g, f), stamp ()), []))) #> perhaps consolidate_global;
       
    66 
       
    67 fun data x = Local_Theory.background_theory (Interp.map (apfst (cons x))) #> perhaps consolidate;
       
    68 fun data_global x = Interp.map (apfst (cons x)) #> perhaps consolidate_global;
       
    69 
       
    70 val init = Theory.at_begin consolidate_global;
       
    71 
       
    72 end;