author | blanchet |
Fri, 05 Sep 2014 00:41:01 +0200 | |
changeset 58188 | cc71d2be4f0a |
parent 58186 | a6c3962ea907 |
permissions | -rw-r--r-- |
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 | 14 |
val interpretation: string -> (T -> theory -> theory) -> (T -> local_theory -> local_theory) -> |
15 |
theory -> theory |
|
58188 | 16 |
val data: (string -> bool) -> T -> local_theory -> local_theory |
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 | 29 |
((Name_Space.naming * (string -> bool)) * T) list |
58186 | 30 |
* ((((T -> theory -> theory) * (T -> local_theory -> local_theory)) * (string * stamp)) |
58188 | 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 | 35 |
(Library.merge (eq_snd eq) (data1, data2), |
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 | 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 | 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 | 44 |
|
45 |
fun unfinished_of ((gf, (name, _)), data') = |
|
58185 | 46 |
(g_or_f gf, |
58188 | 47 |
if eq_list (eq_snd eq) (data', data) then |
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; |
|
58177
166131276380
introduced local interpretation mechanism for BNFs, to solve issues with datatypes in locales
blanchet
parents:
diff
changeset
|
55 |
in |
58182 | 56 |
if forall (null o #2) unfinished then |
57 |
NONE |
|
58 |
else |
|
59 |
SOME (thy_or_lthy |> fold_rev (uncurry fold_rev) unfinished |
|
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 | 63 |
fun consolidate lthy = |
58185 | 64 |
consolidate_common (fn (_, g) => fn (_, x) => g x) Local_Theory.background_theory |
65 |
(Proof_Context.theory_of lthy) lthy; |
|
66 |
||
67 |
fun consolidate_global thy = |
|
68 |
consolidate_common (fn (f, _) => fn (naming, x) => fn thy => |
|
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 | 71 |
fun interpretation name g f = |
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 | 74 |
fun data keep x = |
75 |
Local_Theory.background_theory |
|
76 |
(fn thy => Interp.map (apfst (cons ((Sign.naming_of thy, keep), x))) thy) |
|
58185 | 77 |
#> perhaps consolidate; |
78 |
||
58188 | 79 |
fun data_global keep x = |
80 |
(fn thy => Interp.map (apfst (cons ((Sign.naming_of thy, keep), x))) thy) |
|
58185 | 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; |