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