src/HOL/Tools/Ctr_Sugar/local_interpretation.ML
author blanchet
Fri, 05 Sep 2014 00:41:01 +0200
changeset 58188 cc71d2be4f0a
parent 58186 a6c3962ea907
permissions -rw-r--r--
introduced mechanism to filter interpretations
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58177
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
     1
(*  Title:      HOL/Tools/Ctr_Sugar/local_interpretation.ML
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
     2
    Author:     Jasmin Blanchette, TU Muenchen
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
     3
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
     4
Generic interpretation of local theory data -- ad hoc. Based on
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
     5
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
     6
    Title:      Pure/interpretation.ML
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
     7
    Author:     Florian Haftmann and Makarius
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
     8
*)
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
     9
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    10
signature LOCAL_INTERPRETATION =
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    11
sig
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    12
  type T
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    13
  val result: theory -> T list
58186
a6c3962ea907 named interpretations
blanchet
parents: 58185
diff changeset
    14
  val interpretation: string -> (T -> theory -> theory) -> (T -> local_theory -> local_theory) ->
a6c3962ea907 named interpretations
blanchet
parents: 58185
diff changeset
    15
    theory -> theory
58188
cc71d2be4f0a introduced mechanism to filter interpretations
blanchet
parents: 58186
diff changeset
    16
  val data: (string -> bool) -> T -> local_theory -> local_theory
cc71d2be4f0a introduced mechanism to filter interpretations
blanchet
parents: 58186
diff changeset
    17
  val data_global: (string -> bool) -> T -> theory -> theory
58177
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    18
  val init: theory -> theory
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    19
end;
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    20
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    21
functor Local_Interpretation(type T val eq: T * T -> bool): LOCAL_INTERPRETATION =
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    22
struct
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    23
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    24
type T = T;
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    25
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    26
structure Interp = Theory_Data
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    27
(
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    28
  type T =
58188
cc71d2be4f0a introduced mechanism to filter interpretations
blanchet
parents: 58186
diff changeset
    29
    ((Name_Space.naming * (string -> bool)) * T) list
58186
a6c3962ea907 named interpretations
blanchet
parents: 58185
diff changeset
    30
    * ((((T -> theory -> theory) * (T -> local_theory -> local_theory)) * (string * stamp))
58188
cc71d2be4f0a introduced mechanism to filter interpretations
blanchet
parents: 58186
diff changeset
    31
       * ((Name_Space.naming * (string -> bool)) * T) list) list;
58177
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    32
  val empty = ([], []);
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    33
  val extend = I;
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    34
  fun merge ((data1, interps1), (data2, interps2)) : T =
58185
e6e3b20340be centralized and cleaned up naming handling
blanchet
parents: 58182
diff changeset
    35
    (Library.merge (eq_snd eq) (data1, data2),
e6e3b20340be centralized and cleaned up naming handling
blanchet
parents: 58182
diff changeset
    36
     AList.join (eq_snd (op =)) (K (Library.merge (eq_snd eq))) (interps1, interps2));
58177
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    37
);
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    38
58185
e6e3b20340be centralized and cleaned up naming handling
blanchet
parents: 58182
diff changeset
    39
val result = map snd o fst o Interp.get;
58177
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    40
58178
blanchet
parents: 58177
diff changeset
    41
fun consolidate_common g_or_f lift_lthy thy thy_or_lthy =
58177
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    42
  let
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    43
    val (data, interps) = Interp.get thy;
58188
cc71d2be4f0a introduced mechanism to filter interpretations
blanchet
parents: 58186
diff changeset
    44
cc71d2be4f0a introduced mechanism to filter interpretations
blanchet
parents: 58186
diff changeset
    45
    fun unfinished_of ((gf, (name, _)), data') =
58185
e6e3b20340be centralized and cleaned up naming handling
blanchet
parents: 58182
diff changeset
    46
      (g_or_f gf,
58188
cc71d2be4f0a introduced mechanism to filter interpretations
blanchet
parents: 58186
diff changeset
    47
       if eq_list (eq_snd eq) (data', data) then
cc71d2be4f0a introduced mechanism to filter interpretations
blanchet
parents: 58186
diff changeset
    48
         []
cc71d2be4f0a introduced mechanism to filter interpretations
blanchet
parents: 58186
diff changeset
    49
       else
cc71d2be4f0a introduced mechanism to filter interpretations
blanchet
parents: 58186
diff changeset
    50
         subtract (eq_snd eq) data' data
cc71d2be4f0a introduced mechanism to filter interpretations
blanchet
parents: 58186
diff changeset
    51
         |> map_filter (fn ((naming, keep), x) => if keep name then SOME (naming, x) else NONE));
cc71d2be4f0a introduced mechanism to filter interpretations
blanchet
parents: 58186
diff changeset
    52
cc71d2be4f0a introduced mechanism to filter interpretations
blanchet
parents: 58186
diff changeset
    53
    val unfinished = map unfinished_of interps;
cc71d2be4f0a introduced mechanism to filter interpretations
blanchet
parents: 58186
diff changeset
    54
    val finished = map (apsnd (K data)) interps;
58177
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    55
  in
58182
82478e6c60cb tweaked setup for datatype realizer
blanchet
parents: 58178
diff changeset
    56
    if forall (null o #2) unfinished then
82478e6c60cb tweaked setup for datatype realizer
blanchet
parents: 58178
diff changeset
    57
      NONE
82478e6c60cb tweaked setup for datatype realizer
blanchet
parents: 58178
diff changeset
    58
    else
82478e6c60cb tweaked setup for datatype realizer
blanchet
parents: 58178
diff changeset
    59
      SOME (thy_or_lthy |> fold_rev (uncurry fold_rev) unfinished
82478e6c60cb tweaked setup for datatype realizer
blanchet
parents: 58178
diff changeset
    60
        |> lift_lthy (Interp.put (data, finished)))
58177
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    61
  end;
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    62
58178
blanchet
parents: 58177
diff changeset
    63
fun consolidate lthy =
58185
e6e3b20340be centralized and cleaned up naming handling
blanchet
parents: 58182
diff changeset
    64
  consolidate_common (fn (_, g) => fn (_, x) => g x) Local_Theory.background_theory
e6e3b20340be centralized and cleaned up naming handling
blanchet
parents: 58182
diff changeset
    65
    (Proof_Context.theory_of lthy) lthy;
e6e3b20340be centralized and cleaned up naming handling
blanchet
parents: 58182
diff changeset
    66
e6e3b20340be centralized and cleaned up naming handling
blanchet
parents: 58182
diff changeset
    67
fun consolidate_global thy =
e6e3b20340be centralized and cleaned up naming handling
blanchet
parents: 58182
diff changeset
    68
  consolidate_common (fn (f, _) => fn (naming, x) => fn thy =>
e6e3b20340be centralized and cleaned up naming handling
blanchet
parents: 58182
diff changeset
    69
    thy |> Sign.map_naming (K naming) |> f x |> Sign.restore_naming thy) I thy thy;
58177
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    70
58186
a6c3962ea907 named interpretations
blanchet
parents: 58185
diff changeset
    71
fun interpretation name g f =
a6c3962ea907 named interpretations
blanchet
parents: 58185
diff changeset
    72
  Interp.map (apsnd (cons (((g, f), (name, stamp ())), []))) #> perhaps consolidate_global;
58177
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    73
58188
cc71d2be4f0a introduced mechanism to filter interpretations
blanchet
parents: 58186
diff changeset
    74
fun data keep x =
cc71d2be4f0a introduced mechanism to filter interpretations
blanchet
parents: 58186
diff changeset
    75
  Local_Theory.background_theory
cc71d2be4f0a introduced mechanism to filter interpretations
blanchet
parents: 58186
diff changeset
    76
    (fn thy => Interp.map (apfst (cons ((Sign.naming_of thy, keep), x))) thy)
58185
e6e3b20340be centralized and cleaned up naming handling
blanchet
parents: 58182
diff changeset
    77
  #> perhaps consolidate;
e6e3b20340be centralized and cleaned up naming handling
blanchet
parents: 58182
diff changeset
    78
58188
cc71d2be4f0a introduced mechanism to filter interpretations
blanchet
parents: 58186
diff changeset
    79
fun data_global keep x =
cc71d2be4f0a introduced mechanism to filter interpretations
blanchet
parents: 58186
diff changeset
    80
  (fn thy => Interp.map (apfst (cons ((Sign.naming_of thy, keep), x))) thy)
58185
e6e3b20340be centralized and cleaned up naming handling
blanchet
parents: 58182
diff changeset
    81
  #> perhaps consolidate_global;
58177
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    82
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    83
val init = Theory.at_begin consolidate_global;
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    84
166131276380 introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff changeset
    85
end;