src/HOL/Tools/Ctr_Sugar/local_interpretation.ML
changeset 58188 cc71d2be4f0a
parent 58186 a6c3962ea907
equal deleted inserted replaced
58187:d2ddd401d74d 58188:cc71d2be4f0a
    11 sig
    11 sig
    12   type T
    12   type T
    13   val result: theory -> T list
    13   val result: theory -> T list
    14   val interpretation: string -> (T -> theory -> theory) -> (T -> local_theory -> local_theory) ->
    14   val interpretation: string -> (T -> theory -> theory) -> (T -> local_theory -> local_theory) ->
    15     theory -> theory
    15     theory -> theory
    16   val data: T -> local_theory -> local_theory
    16   val data: (string -> bool) -> T -> local_theory -> local_theory
    17   val data_global: T -> theory -> theory
    17   val data_global: (string -> bool) -> T -> theory -> theory
    18   val init: theory -> theory
    18   val init: theory -> theory
    19 end;
    19 end;
    20 
    20 
    21 functor Local_Interpretation(type T val eq: T * T -> bool): LOCAL_INTERPRETATION =
    21 functor Local_Interpretation(type T val eq: T * T -> bool): LOCAL_INTERPRETATION =
    22 struct
    22 struct
    24 type T = T;
    24 type T = T;
    25 
    25 
    26 structure Interp = Theory_Data
    26 structure Interp = Theory_Data
    27 (
    27 (
    28   type T =
    28   type T =
    29     (Name_Space.naming * T) list
    29     ((Name_Space.naming * (string -> bool)) * T) list
    30     * ((((T -> theory -> theory) * (T -> local_theory -> local_theory)) * (string * stamp))
    30     * ((((T -> theory -> theory) * (T -> local_theory -> local_theory)) * (string * stamp))
    31        * (Name_Space.naming * T) list) list;
    31        * ((Name_Space.naming * (string -> bool)) * T) list) list;
    32   val empty = ([], []);
    32   val empty = ([], []);
    33   val extend = I;
    33   val extend = I;
    34   fun merge ((data1, interps1), (data2, interps2)) : T =
    34   fun merge ((data1, interps1), (data2, interps2)) : T =
    35     (Library.merge (eq_snd eq) (data1, data2),
    35     (Library.merge (eq_snd eq) (data1, data2),
    36      AList.join (eq_snd (op =)) (K (Library.merge (eq_snd eq))) (interps1, interps2));
    36      AList.join (eq_snd (op =)) (K (Library.merge (eq_snd eq))) (interps1, interps2));
    39 val result = map snd o fst o Interp.get;
    39 val result = map snd o fst o Interp.get;
    40 
    40 
    41 fun consolidate_common g_or_f lift_lthy thy thy_or_lthy =
    41 fun consolidate_common g_or_f lift_lthy thy thy_or_lthy =
    42   let
    42   let
    43     val (data, interps) = Interp.get thy;
    43     val (data, interps) = Interp.get thy;
    44     val unfinished = interps |> map (fn ((gf, _), data') =>
    44 
       
    45     fun unfinished_of ((gf, (name, _)), data') =
    45       (g_or_f gf,
    46       (g_or_f gf,
    46        if eq_list (eq_snd eq) (data', data) then [] else subtract (eq_snd eq) data' data));
    47        if eq_list (eq_snd eq) (data', data) then
    47     val finished = interps |> map (apsnd (K data));
    48          []
       
    49        else
       
    50          subtract (eq_snd eq) data' data
       
    51          |> map_filter (fn ((naming, keep), x) => if keep name then SOME (naming, x) else NONE));
       
    52 
       
    53     val unfinished = map unfinished_of interps;
       
    54     val finished = map (apsnd (K data)) interps;
    48   in
    55   in
    49     if forall (null o #2) unfinished then
    56     if forall (null o #2) unfinished then
    50       NONE
    57       NONE
    51     else
    58     else
    52       SOME (thy_or_lthy |> fold_rev (uncurry fold_rev) unfinished
    59       SOME (thy_or_lthy |> fold_rev (uncurry fold_rev) unfinished
    62     thy |> Sign.map_naming (K naming) |> f x |> Sign.restore_naming thy) I thy thy;
    69     thy |> Sign.map_naming (K naming) |> f x |> Sign.restore_naming thy) I thy thy;
    63 
    70 
    64 fun interpretation name g f =
    71 fun interpretation name g f =
    65   Interp.map (apsnd (cons (((g, f), (name, stamp ())), []))) #> perhaps consolidate_global;
    72   Interp.map (apsnd (cons (((g, f), (name, stamp ())), []))) #> perhaps consolidate_global;
    66 
    73 
    67 fun data x =
    74 fun data keep x =
    68   Local_Theory.background_theory (fn thy => Interp.map (apfst (cons (Sign.naming_of thy, x))) thy)
    75   Local_Theory.background_theory
       
    76     (fn thy => Interp.map (apfst (cons ((Sign.naming_of thy, keep), x))) thy)
    69   #> perhaps consolidate;
    77   #> perhaps consolidate;
    70 
    78 
    71 fun data_global x =
    79 fun data_global keep x =
    72   (fn thy => Interp.map (apfst (cons (Sign.naming_of thy, x))) thy)
    80   (fn thy => Interp.map (apfst (cons ((Sign.naming_of thy, keep), x))) thy)
    73   #> perhaps consolidate_global;
    81   #> perhaps consolidate_global;
    74 
    82 
    75 val init = Theory.at_begin consolidate_global;
    83 val init = Theory.at_begin consolidate_global;
    76 
    84 
    77 end;
    85 end;