| author | nipkow |
| Sat, 25 Jan 2014 19:07:07 +0100 | |
| changeset 55132 | ee5a0ca00b6f |
| parent 55042 | 29e1657b7389 |
| child 55147 | bce3dbc11f95 |
| permissions | -rw-r--r-- |
| 52265 | 1 |
(* Title: Tools/Code/code_symbol.ML |
| 52136 | 2 |
Author: Florian Haftmann, TU Muenchen |
3 |
||
4 |
Data related to symbols in programs: constants, type constructors, classes, |
|
5 |
class relations, class instances, modules. |
|
6 |
*) |
|
7 |
||
8 |
signature CODE_SYMBOL = |
|
9 |
sig |
|
10 |
datatype ('a, 'b, 'c, 'd, 'e, 'f) attr =
|
|
11 |
Constant of 'a | Type_Constructor of 'b | Type_Class of 'c | |
|
12 |
Class_Relation of 'd | Class_Instance of 'e | Module of 'f |
|
| 54933 | 13 |
type symbol = (string, string, class, class * class, string * class, string) attr |
| 55042 | 14 |
structure Table: TABLE; |
| 54933 | 15 |
structure Graph: GRAPH; |
| 54988 | 16 |
val default_name: Proof.context -> symbol -> string * string |
17 |
val quote_symbol: Proof.context -> symbol -> string |
|
| 54933 | 18 |
val tyco_fun: string |
| 52136 | 19 |
val map_attr: ('a -> 'g) -> ('b -> 'h) -> ('c -> 'i) -> ('d -> 'j) -> ('e -> 'k) -> ('f -> 'l)
|
20 |
-> ('a, 'b, 'c, 'd, 'e, 'f) attr -> ('g, 'h, 'i, 'j, 'k, 'l) attr
|
|
21 |
val maps_attr: ('a -> 'g list) -> ('b -> 'h list)
|
|
22 |
-> ('c -> 'i list) -> ('d -> 'j list) -> ('e -> 'k list) -> ('f -> 'l list)
|
|
23 |
-> ('a, 'b, 'c, 'd, 'e, 'f) attr -> ('g, 'h, 'i, 'j, 'k, 'l) attr list
|
|
24 |
val maps_attr': ('a -> ('m * 'g) list) -> ('b -> ('m * 'h) list)
|
|
25 |
-> ('c -> ('m * 'i) list) -> ('d -> ('m * 'j) list) -> ('e -> ('m * 'k) list) -> ('f -> ('m * 'l) list)
|
|
26 |
-> ('a, 'b, 'c, 'd, 'e, 'f) attr -> ('m * ('g, 'h, 'i, 'j, 'k, 'l) attr) list
|
|
27 |
type ('a, 'b, 'c, 'd, 'e, 'f) data
|
|
28 |
val empty_data: ('a, 'b, 'c, 'd, 'e, 'f) data
|
|
29 |
val merge_data: ('a, 'b, 'c, 'd, 'e, 'f) data * ('a, 'b, 'c, 'd, 'e, 'f) data
|
|
30 |
-> ('a, 'b, 'c, 'd, 'e, 'f) data
|
|
|
52138
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
31 |
val set_data: (string * 'a option, string * 'b option, string * 'c option, |
|
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
32 |
(class * class) * 'd option, (string * class) * 'e option, string * 'f option) attr |
|
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
33 |
-> ('a, 'b, 'c, 'd, 'e, 'f) data -> ('a, 'b, 'c, 'd, 'e, 'f) data
|
| 52136 | 34 |
val lookup_constant_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> string -> 'a option
|
35 |
val lookup_type_constructor_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> string -> 'b option
|
|
36 |
val lookup_type_class_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> class -> 'c option
|
|
37 |
val lookup_class_relation_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> class * class -> 'd option
|
|
38 |
val lookup_class_instance_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> string * class -> 'e option
|
|
39 |
val lookup_module_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> string -> 'f option
|
|
|
52138
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
40 |
val lookup: ('a, 'a, 'a, 'a, 'a, 'a) data -> symbol -> 'a option
|
| 52136 | 41 |
val dest_constant_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> (string * 'a) list
|
42 |
val dest_type_constructor_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> (string * 'b) list
|
|
43 |
val dest_type_class_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> (class * 'c) list
|
|
44 |
val dest_class_relation_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> ((class * class) * 'd) list
|
|
45 |
val dest_class_instance_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> ((string * class) * 'e) list
|
|
46 |
val dest_module_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> (string * 'f) list
|
|
47 |
end; |
|
48 |
||
49 |
structure Code_Symbol : CODE_SYMBOL = |
|
50 |
struct |
|
51 |
||
52 |
(* data for symbols *) |
|
53 |
||
54 |
datatype ('a, 'b, 'c, 'd, 'e, 'f) attr =
|
|
55 |
Constant of 'a | Type_Constructor of 'b | Type_Class of 'c | Class_Relation of 'd | Class_Instance of 'e | Module of 'f; |
|
56 |
||
|
52138
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
57 |
type symbol = (string, string, class, string * class, class * class, string) attr; |
|
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
58 |
|
| 54933 | 59 |
fun symbol_ord (Constant c1, Constant c2) = fast_string_ord (c1, c2) |
60 |
| symbol_ord (Constant _, _) = GREATER |
|
61 |
| symbol_ord (_, Constant _) = LESS |
|
62 |
| symbol_ord (Type_Constructor tyco1, Type_Constructor tyco2) = fast_string_ord (tyco1, tyco2) |
|
63 |
| symbol_ord (Type_Constructor _, _) = GREATER |
|
64 |
| symbol_ord (_, Type_Constructor _) = LESS |
|
65 |
| symbol_ord (Type_Class class1, Type_Class class2) = fast_string_ord (class1, class2) |
|
66 |
| symbol_ord (Type_Class _, _) = GREATER |
|
67 |
| symbol_ord (_, Type_Class _) = LESS |
|
68 |
| symbol_ord (Class_Relation classrel1, Class_Relation classrel2) = |
|
69 |
prod_ord fast_string_ord fast_string_ord (classrel1, classrel2) |
|
70 |
| symbol_ord (Class_Relation _, _) = GREATER |
|
71 |
| symbol_ord (_, Class_Relation _) = LESS |
|
72 |
| symbol_ord (Class_Instance inst1, Class_Instance inst2) = |
|
73 |
prod_ord fast_string_ord fast_string_ord (inst1, inst2) |
|
74 |
| symbol_ord (Class_Instance _, _) = GREATER |
|
75 |
| symbol_ord (_, Class_Instance _) = LESS |
|
76 |
| symbol_ord (Module name1, Module name2) = fast_string_ord (name1, name2); |
|
77 |
||
| 55042 | 78 |
structure Table = Table(type key = symbol val ord = symbol_ord); |
| 54933 | 79 |
structure Graph = Graph(type key = symbol val ord = symbol_ord); |
80 |
||
81 |
local |
|
82 |
fun thyname_of_type thy = #theory_name o Name_Space.the_entry (Sign.type_space thy); |
|
83 |
fun thyname_of_class thy = #theory_name o Name_Space.the_entry (Sign.class_space thy); |
|
84 |
fun thyname_of_instance thy inst = case Axclass.thynames_of_arity thy inst |
|
85 |
of [] => error ("No such instance: " ^ quote (fst inst ^ " :: " ^ snd inst))
|
|
86 |
| thyname :: _ => thyname; |
|
87 |
fun thyname_of_const thy c = case Axclass.class_of_param thy c |
|
88 |
of SOME class => thyname_of_class thy class |
|
89 |
| NONE => (case Code.get_type_of_constr_or_abstr thy c |
|
90 |
of SOME (tyco, _) => thyname_of_type thy tyco |
|
91 |
| NONE => #theory_name (Name_Space.the_entry (Sign.const_space thy) c)); |
|
92 |
fun base_rel (x, y) = Long_Name.base_name y ^ "_" ^ Long_Name.base_name x; |
|
93 |
fun plainify ctxt get_prefix get_basename thing = |
|
| 54988 | 94 |
(get_prefix (Proof_Context.theory_of ctxt) thing, |
95 |
Name.desymbolize false (get_basename thing)); |
|
| 54933 | 96 |
in |
97 |
||
| 54988 | 98 |
fun default_name ctxt (Constant "==>") = |
| 54933 | 99 |
plainify ctxt thyname_of_const (K "follows") "==>" |
| 54988 | 100 |
| default_name ctxt (Constant "==") = |
| 54933 | 101 |
plainify ctxt thyname_of_const (K "meta_eq") "==" |
| 54988 | 102 |
| default_name ctxt (Constant c) = |
| 54933 | 103 |
plainify ctxt thyname_of_const Long_Name.base_name c |
| 54988 | 104 |
| default_name ctxt (Type_Constructor "fun") = |
| 54933 | 105 |
plainify ctxt thyname_of_type (K "fun") "fun" |
| 54988 | 106 |
| default_name ctxt (Type_Constructor tyco) = |
| 54933 | 107 |
plainify ctxt thyname_of_type Long_Name.base_name tyco |
| 54988 | 108 |
| default_name ctxt (Type_Class class) = |
| 54933 | 109 |
plainify ctxt thyname_of_class Long_Name.base_name class |
| 54988 | 110 |
| default_name ctxt (Class_Relation classrel) = |
| 54933 | 111 |
plainify ctxt (fn thy => fn (class, _) => thyname_of_class thy class) base_rel classrel |
| 54988 | 112 |
| default_name ctxt (Class_Instance inst) = |
| 54933 | 113 |
plainify ctxt thyname_of_instance base_rel inst; |
114 |
||
| 54988 | 115 |
val tyco_fun = (uncurry Long_Name.append o default_name @{context}) (Type_Constructor "fun");
|
| 54933 | 116 |
|
117 |
end; |
|
118 |
||
119 |
fun quote_symbol ctxt (Constant c) = quote (Code.string_of_const (Proof_Context.theory_of ctxt) c) |
|
120 |
| quote_symbol ctxt (Type_Constructor tyco) = "type " ^ quote (Proof_Context.extern_type ctxt tyco) |
|
121 |
| quote_symbol ctxt (Type_Class class) = "class " ^ quote (Proof_Context.extern_class ctxt class) |
|
122 |
| quote_symbol ctxt (Class_Relation (sub, super)) = |
|
123 |
quote (Proof_Context.extern_class ctxt sub) ^ " < " ^ quote (Proof_Context.extern_class ctxt super) |
|
124 |
| quote_symbol ctxt (Class_Instance (tyco, class)) = |
|
125 |
quote (Proof_Context.extern_type ctxt tyco) ^ " :: " ^ quote (Proof_Context.extern_class ctxt class); |
|
126 |
||
| 52136 | 127 |
fun map_attr const tyco class classrel inst module (Constant x) = Constant (const x) |
128 |
| map_attr const tyco class classrel inst module (Type_Constructor x) = Type_Constructor (tyco x) |
|
129 |
| map_attr const tyco class classrel inst module (Type_Class x) = Type_Class (class x) |
|
130 |
| map_attr const tyco class classrel inst module (Class_Relation x) = Class_Relation (classrel x) |
|
131 |
| map_attr const tyco class classrel inst module (Class_Instance x) = Class_Instance (inst x) |
|
132 |
| map_attr const tyco class classrel inst module (Module x) = Module (module x); |
|
133 |
||
134 |
fun maps_attr const tyco class classrel inst module (Constant x) = map Constant (const x) |
|
135 |
| maps_attr const tyco class classrel inst module (Type_Constructor x) = map Type_Constructor (tyco x) |
|
136 |
| maps_attr const tyco class classrel inst module (Type_Class x) = map Type_Class (class x) |
|
137 |
| maps_attr const tyco class classrel inst module (Class_Relation x) = map Class_Relation (classrel x) |
|
138 |
| maps_attr const tyco class classrel inst module (Class_Instance x) = map Class_Instance (inst x) |
|
139 |
| maps_attr const tyco class classrel inst module (Module x) = map Module (module x); |
|
140 |
||
141 |
fun maps_attr' const tyco class classrel inst module (Constant x) = (map o apsnd) Constant (const x) |
|
142 |
| maps_attr' const tyco class classrel inst module (Type_Constructor x) = (map o apsnd) Type_Constructor (tyco x) |
|
143 |
| maps_attr' const tyco class classrel inst module (Type_Class x) = (map o apsnd) Type_Class (class x) |
|
144 |
| maps_attr' const tyco class classrel inst module (Class_Relation x) = (map o apsnd) Class_Relation (classrel x) |
|
145 |
| maps_attr' const tyco class classrel inst module (Class_Instance x) = (map o apsnd) Class_Instance (inst x) |
|
146 |
| maps_attr' const tyco class classrel inst module (Module x) = (map o apsnd) Module (module x); |
|
147 |
||
148 |
datatype ('a, 'b, 'c, 'd, 'e, 'f) data =
|
|
149 |
Data of { constant: 'a Symtab.table, type_constructor: 'b Symtab.table,
|
|
150 |
type_class: 'c Symtab.table, class_relation: 'd Symreltab.table, class_instance: 'e Symreltab.table, |
|
151 |
module: 'f Symtab.table }; |
|
152 |
||
153 |
fun make_data constant type_constructor type_class class_relation class_instance module = |
|
154 |
Data { constant = constant, type_constructor = type_constructor,
|
|
155 |
type_class = type_class, class_relation = class_relation, class_instance = class_instance, module = module }; |
|
156 |
fun dest_data (Data x) = x; |
|
157 |
fun map_data map_constant map_type_constructor map_type_class map_class_relation map_class_instance map_module |
|
158 |
(Data { constant, type_constructor, type_class, class_relation, class_instance, module }) =
|
|
159 |
Data { constant = map_constant constant, type_constructor = map_type_constructor type_constructor,
|
|
160 |
type_class = map_type_class type_class, class_relation = map_class_relation class_relation, |
|
161 |
class_instance = map_class_instance class_instance, module = map_module module }; |
|
162 |
||
163 |
val empty_data = Data { constant = Symtab.empty, type_constructor = Symtab.empty,
|
|
164 |
type_class = Symtab.empty, class_relation = Symreltab.empty, class_instance = Symreltab.empty, module = Symtab.empty }; |
|
165 |
fun merge_data (Data { constant = constant1, type_constructor = type_constructor1,
|
|
166 |
type_class = type_class1, class_instance = class_instance1, class_relation = class_relation1, module = module1 }, |
|
167 |
Data { constant = constant2, type_constructor = type_constructor2,
|
|
168 |
type_class = type_class2, class_instance = class_instance2, class_relation = class_relation2, module = module2 }) = |
|
169 |
make_data (Symtab.join (K snd) (constant1, constant2)) |
|
170 |
(Symtab.join (K snd) (type_constructor1, type_constructor2)) |
|
171 |
(Symtab.join (K snd) (type_class1, type_class2)) |
|
172 |
(Symreltab.join (K snd) (class_relation1, class_relation2)) |
|
173 |
(Symreltab.join (K snd) (class_instance1, class_instance2)) |
|
174 |
(Symtab.join (K snd) (module1, module2)); (*prefer later entries: K snd*) |
|
175 |
||
176 |
fun set_sym (sym, NONE) = Symtab.delete_safe sym |
|
177 |
| set_sym (sym, SOME y) = Symtab.update (sym, y); |
|
178 |
fun set_symrel (symrel, NONE) = Symreltab.delete_safe symrel |
|
179 |
| set_symrel (symrel, SOME y) = Symreltab.update (symrel, y); |
|
180 |
||
181 |
fun set_data (Constant x) = map_data (set_sym x) I I I I I |
|
182 |
| set_data (Type_Constructor x) = map_data I (set_sym x) I I I I |
|
183 |
| set_data (Type_Class x) = map_data I I (set_sym x) I I I |
|
184 |
| set_data (Class_Relation x) = map_data I I I (set_symrel x) I I |
|
185 |
| set_data (Class_Instance x) = map_data I I I I (set_symrel x) I |
|
186 |
| set_data (Module x) = map_data I I I I I (set_sym x); |
|
187 |
||
|
52138
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
188 |
fun lookup_constant_data x = (Symtab.lookup o #constant o dest_data) x; |
|
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
189 |
fun lookup_type_constructor_data x = (Symtab.lookup o #type_constructor o dest_data) x; |
|
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
190 |
fun lookup_type_class_data x = (Symtab.lookup o #type_class o dest_data) x; |
|
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
191 |
fun lookup_class_relation_data x = (Symreltab.lookup o #class_relation o dest_data) x; |
|
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
192 |
fun lookup_class_instance_data x = (Symreltab.lookup o #class_instance o dest_data) x; |
|
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
193 |
fun lookup_module_data x = (Symtab.lookup o #module o dest_data) x; |
|
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
194 |
|
|
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
195 |
fun lookup data (Constant x) = lookup_constant_data data x |
|
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
196 |
| lookup data (Type_Constructor x) = lookup_type_constructor_data data x |
|
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
197 |
| lookup data (Type_Class x) = lookup_type_class_data data x |
|
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
198 |
| lookup data (Class_Relation x) = lookup_class_relation_data data x |
|
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
199 |
| lookup data (Class_Instance x) = lookup_class_instance_data data x |
|
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
200 |
| lookup data (Module x) = lookup_module_data data x; |
|
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
201 |
|
|
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
202 |
fun dest_constant_data x = (Symtab.dest o #constant o dest_data) x; |
|
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
203 |
fun dest_type_constructor_data x = (Symtab.dest o #type_constructor o dest_data) x; |
|
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
204 |
fun dest_type_class_data x = (Symtab.dest o #type_class o dest_data) x; |
|
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
205 |
fun dest_class_relation_data x = (Symreltab.dest o #class_relation o dest_data) x; |
|
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
206 |
fun dest_class_instance_data x = (Symreltab.dest o #class_instance o dest_data) x; |
|
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
207 |
fun dest_module_data x = (Symtab.dest o #module o dest_data) x; |
|
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
52136
diff
changeset
|
208 |
|
| 52136 | 209 |
end; |