src/Tools/code/code_name.ML
author haftmann
Fri, 10 Aug 2007 17:04:34 +0200
changeset 24219 e558fe311376
child 24423 ae9cd0e92423
permissions -rw-r--r--
new structure for code generator modules
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     1
(*  Title:      Tools/code/code_name.ML
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     2
    ID:         $Id$
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     4
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     5
Naming policies for code generation: prefixing any name by corresponding
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     6
theory name, conversion to alphanumeric representation.
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     7
Mappings are incrementally cached.
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     8
*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     9
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    10
signature CODE_NAME =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    11
sig
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    12
  val purify_var: string -> string
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    13
  val purify_tvar: string -> string
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    14
  val check_modulename: string -> string
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    15
  type var_ctxt;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    16
  val make_vars: string list -> var_ctxt;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    17
  val intro_vars: string list -> var_ctxt -> var_ctxt;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    18
  val lookup_var: var_ctxt -> string -> string;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    19
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    20
  type tyco = string
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    21
  type const = string
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    22
  val class: theory -> class -> class
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    23
  val class_rev: theory -> class -> class option
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    24
  val classrel: theory -> class * class -> string
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    25
  val classrel_rev: theory -> string -> (class * class) option
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    26
  val tyco: theory -> tyco -> tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    27
  val tyco_rev: theory -> tyco -> tyco option
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    28
  val instance: theory -> class * tyco -> string
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    29
  val instance_rev: theory -> string -> (class * tyco) option
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    30
  val const: theory -> CodeUnit.const -> const
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    31
  val const_rev: theory -> const -> CodeUnit.const option
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    32
  val labelled_name: theory -> string -> string
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    33
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    34
  val setup: theory -> theory
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    35
end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    36
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    37
structure CodeName: CODE_NAME =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    38
struct
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    39
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    40
(** purification **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    41
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    42
fun purify_name upper_else_lower =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    43
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    44
    fun is_valid s = Symbol.is_ascii_letter s orelse Symbol.is_ascii_digit s orelse s = "'";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    45
    val is_junk = not o is_valid andf Symbol.is_regular;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    46
    val junk = Scan.many is_junk;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    47
    val scan_valids = Symbol.scanner "Malformed input"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    48
      ((junk |--
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    49
        (Scan.optional (Scan.one Symbol.is_ascii_letter) "x" ^^ (Scan.many is_valid >> implode)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    50
        --| junk))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    51
      -- Scan.repeat ((Scan.many1 is_valid >> implode) --| junk) >> op ::);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    52
    fun upper_lower cs = if upper_else_lower then nth_map 0 Symbol.to_ascii_upper cs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    53
      else (if forall Symbol.is_ascii_upper cs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    54
        then map else nth_map 0) Symbol.to_ascii_lower cs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    55
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    56
    explode
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    57
    #> scan_valids
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    58
    #> space_implode "_"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    59
    #> explode
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    60
    #> upper_lower
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    61
    #> implode
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    62
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    63
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    64
fun purify_var "" = "x"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    65
  | purify_var v = purify_name false v;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    66
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    67
fun purify_tvar "" = "'a"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    68
  | purify_tvar v =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    69
      (unprefix "'" #> explode #> filter Symbol.is_ascii_letter #> cons "'" #> implode) v;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    70
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    71
fun check_modulename mn =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    72
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    73
    val mns = NameSpace.explode mn;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    74
    val mns' = map (purify_name true) mns;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    75
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    76
    if mns' = mns then mn else error ("Invalid module name: " ^ quote mn ^ "\n"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    77
      ^ "perhaps try " ^ quote (NameSpace.implode mns'))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    78
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    79
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    80
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    81
(** variable name contexts **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    82
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    83
type var_ctxt = string Symtab.table * Name.context;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    84
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    85
fun make_vars names = (fold (fn name => Symtab.update_new (name, name)) names Symtab.empty,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    86
  Name.make_context names);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    87
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    88
fun intro_vars names (namemap, namectxt) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    89
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    90
    val (names', namectxt') = Name.variants names namectxt;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    91
    val namemap' = fold2 (curry Symtab.update) names names' namemap;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    92
  in (namemap', namectxt') end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    93
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    94
fun lookup_var (namemap, _) name = case Symtab.lookup namemap name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    95
 of SOME name' => name'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    96
  | NONE => error ("Invalid name in context: " ^ quote name);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    97
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    98
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    99
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   100
(** global names (identifiers) **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   101
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   102
(* identifier categories *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   103
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   104
val idf_class = "class";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   105
val idf_classrel = "clsrel"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   106
val idf_tyco = "tyco";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   107
val idf_instance = "inst";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   108
val idf_const = "const";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   109
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   110
fun string_of_classrel (class, superclass) = class ^ " < " ^ superclass;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   111
fun string_of_instance (class, tyco) = tyco ^ " :: " ^ class;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   112
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   113
fun add_idf nsp name =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   114
  NameSpace.append name nsp;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   115
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   116
fun dest_idf nsp name =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   117
  if NameSpace.base name = nsp
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   118
  then SOME (NameSpace.qualifier name)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   119
  else NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   120
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   121
local
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   122
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   123
val name_mapping  = [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   124
  (idf_class,       "class"),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   125
  (idf_classrel,    "subclass relation"),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   126
  (idf_tyco,        "type constructor"),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   127
  (idf_instance,    "instance"),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   128
  (idf_const,       "constant")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   129
]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   130
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   131
in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   132
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   133
val category_of = the o AList.lookup (op =) name_mapping o NameSpace.base;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   134
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   135
end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   136
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   137
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   138
(* theory name lookup *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   139
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   140
fun thyname_of thy f errmsg x =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   141
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   142
    fun thy_of thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   143
      if f thy x then case get_first thy_of (Theory.parents_of thy)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   144
        of NONE => SOME thy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   145
         | thy => thy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   146
      else NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   147
  in case thy_of thy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   148
   of SOME thy => Context.theory_name thy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   149
    | NONE => error (errmsg x) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   150
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   151
fun thyname_of_class thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   152
  thyname_of thy (fn thy => member (op =) (Sign.all_classes thy))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   153
    (fn class => "thyname_of_class: no such class: " ^ quote class);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   154
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   155
fun thyname_of_classrel thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   156
  thyname_of thy (fn thy => fn (class1, class2) => Sign.subsort thy ([class1], [class2]))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   157
    (fn (class1, class2) => "thyname_of_classrel: no such subclass relation: "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   158
      ^ (quote o string_of_classrel) (class1, class2));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   159
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   160
fun thyname_of_tyco thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   161
  thyname_of thy Sign.declared_tyname
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   162
    (fn tyco => "thyname_of_tyco: no such type constructor: " ^ quote tyco);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   163
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   164
fun thyname_of_instance thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   165
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   166
    fun test_instance thy (class, tyco) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   167
      can (Sorts.mg_domain (Sign.classes_of thy) tyco) [class]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   168
  in thyname_of thy test_instance
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   169
    (fn (class, tyco) => "thyname_of_instance: no such instance: "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   170
      ^ (quote o string_of_instance) (class, tyco))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   171
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   172
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   173
fun thyname_of_const thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   174
  thyname_of thy Sign.declared_const
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   175
    (fn c => "thyname_of_const: no such constant: " ^ quote c);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   176
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   177
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   178
(* naming policies *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   179
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   180
val purify_prefix =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   181
  explode
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   182
  (*should disappear as soon as hierarchical theory name spaces are available*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   183
  #> Symbol.scanner "Malformed name"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   184
      (Scan.repeat ($$ "_" |-- $$ "_" >> (fn _ => ".") || Scan.one Symbol.is_regular))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   185
  #> implode
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   186
  #> NameSpace.explode
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   187
  #> map (purify_name true);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   188
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   189
fun purify_base "op =" = "eq"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   190
  | purify_base "op &" = "and"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   191
  | purify_base "op |" = "or"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   192
  | purify_base "op -->" = "implies"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   193
  | purify_base "{}" = "empty"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   194
  | purify_base "op :" = "member"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   195
  | purify_base "op Int" = "intersect"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   196
  | purify_base "op Un" = "union"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   197
  | purify_base "*" = "product"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   198
  | purify_base "+" = "sum"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   199
  | purify_base s = purify_name false s;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   200
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   201
fun default_policy thy get_basename get_thyname name =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   202
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   203
    val prefix = (purify_prefix o get_thyname thy) name;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   204
    val base = (purify_base o get_basename) name;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   205
  in NameSpace.implode (prefix @ [base]) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   206
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   207
fun class_policy thy = default_policy thy NameSpace.base thyname_of_class;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   208
fun classrel_policy thy = default_policy thy (fn (class1, class2) => 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   209
  NameSpace.base class2 ^ "_" ^ NameSpace.base class1) thyname_of_classrel;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   210
  (*order fits nicely with composed projections*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   211
fun tyco_policy thy = default_policy thy NameSpace.base thyname_of_tyco;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   212
fun instance_policy thy = default_policy thy (fn (class, tyco) => 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   213
  NameSpace.base class ^ "_" ^ NameSpace.base tyco) thyname_of_instance;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   214
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   215
fun force_thyname thy (const as (c, opt_tyco)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   216
  case Code.get_datatype_of_constr thy const
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   217
   of SOME dtco => SOME (thyname_of_tyco thy dtco)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   218
    | NONE => (case AxClass.class_of_param thy c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   219
       of SOME class => (case opt_tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   220
           of SOME tyco => SOME (thyname_of_instance thy (class, tyco))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   221
            | NONE => SOME (thyname_of_class thy class))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   222
        | NONE => NONE);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   223
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   224
fun const_policy thy (const as (c, opt_tyco)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   225
  case force_thyname thy const
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   226
   of NONE => default_policy thy NameSpace.base thyname_of_const c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   227
    | SOME thyname => let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   228
        val prefix = purify_prefix thyname;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   229
        val tycos = the_list opt_tyco;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   230
        val base = map (purify_base o NameSpace.base) (c :: tycos);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   231
      in NameSpace.implode (prefix @ [space_implode "_" base]) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   232
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   233
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   234
(* theory and code data *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   235
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   236
type tyco = string;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   237
type const = string;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   238
structure Consttab = CodeUnit.Consttab;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   239
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   240
structure StringPairTab =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   241
  TableFun(
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   242
    type key = string * string;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   243
    val ord = prod_ord fast_string_ord fast_string_ord;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   244
  );
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   245
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   246
datatype names = Names of {
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   247
  class: class Symtab.table * class Symtab.table,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   248
  classrel: string StringPairTab.table * (class * class) Symtab.table,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   249
  tyco: tyco Symtab.table * tyco Symtab.table,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   250
  instance: string StringPairTab.table * (class * tyco) Symtab.table
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   251
}
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   252
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   253
val empty_names = Names {
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   254
  class = (Symtab.empty, Symtab.empty),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   255
  classrel = (StringPairTab.empty, Symtab.empty),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   256
  tyco = (Symtab.empty, Symtab.empty),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   257
  instance = (StringPairTab.empty, Symtab.empty)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   258
};
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   259
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   260
local
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   261
  fun mk_names (class, classrel, tyco, instance) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   262
    Names { class = class, classrel = classrel, tyco = tyco, instance = instance };
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   263
  fun map_names f (Names { class, classrel, tyco, instance }) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   264
    mk_names (f (class, classrel, tyco, instance));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   265
in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   266
  fun merge_names (Names { class = (class1, classrev1),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   267
      classrel = (classrel1, classrelrev1), tyco = (tyco1, tycorev1),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   268
      instance = (instance1, instancerev1) },
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   269
    Names { class = (class2, classrev2),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   270
      classrel = (classrel2, classrelrev2), tyco = (tyco2, tycorev2),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   271
      instance = (instance2, instancerev2) }) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   272
    mk_names ((Symtab.merge (op =) (class1, class2), Symtab.merge (op =) (classrev1, classrev2)),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   273
      (StringPairTab.merge (op =) (classrel1, classrel2), Symtab.merge (op =) (classrelrev1, classrelrev2)),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   274
      (Symtab.merge (op =) (tyco1, tyco2), Symtab.merge (op =) (tycorev1, tycorev2)),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   275
      (StringPairTab.merge (op =) (instance1, instance2), Symtab.merge (op =) (instancerev1, instancerev2)));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   276
  fun map_class f = map_names
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   277
    (fn (class, classrel, tyco, inst) => (f class, classrel, tyco, inst));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   278
  fun map_classrel f = map_names
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   279
    (fn (class, classrel, tyco, inst) => (class, f classrel, tyco, inst));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   280
  fun map_tyco f = map_names
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   281
    (fn (class, classrel, tyco, inst) => (class, classrel, f tyco, inst));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   282
  fun map_instance f = map_names
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   283
    (fn (class, classrel, tyco, inst) => (class, classrel, tyco, f inst));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   284
end; (*local*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   285
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   286
structure CodeName = TheoryDataFun
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   287
(
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   288
  type T = names ref;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   289
  val empty = ref empty_names;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   290
  fun copy (ref names) = ref names;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   291
  val extend = copy;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   292
  fun merge _ (ref names1, ref names2) = ref (merge_names (names1, names2));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   293
);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   294
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   295
structure ConstName = CodeDataFun
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   296
(
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   297
  type T = const Consttab.table * (string * string option) Symtab.table;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   298
  val empty = (Consttab.empty, Symtab.empty);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   299
  fun merge _ ((const1, constrev1), (const2, constrev2)) : T =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   300
    (Consttab.merge (op =) (const1, const2),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   301
      Symtab.merge CodeUnit.eq_const (constrev1, constrev2));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   302
  fun purge _ NONE _ = empty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   303
    | purge _ (SOME cs) (const, constrev) = (fold Consttab.delete_safe cs const,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   304
        fold Symtab.delete (map_filter (Consttab.lookup const) cs) constrev);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   305
);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   306
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   307
val setup = CodeName.init;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   308
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   309
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   310
(* forward lookup with cache update *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   311
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   312
fun get thy get_tabs get upd_names upd policy x =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   313
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   314
    val names_ref = CodeName.get thy;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   315
    val (Names names) = ! names_ref;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   316
    val tabs = get_tabs names;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   317
    fun declare name =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   318
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   319
        val names' = upd_names (K (upd (x, name) (fst tabs),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   320
          Symtab.update_new (name, x) (snd tabs))) (Names names)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   321
      in (names_ref := names'; name) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   322
  in case get (fst tabs) x
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   323
   of SOME name => name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   324
    | NONE => 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   325
        x
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   326
        |> policy thy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   327
        |> Name.variant (Symtab.keys (snd tabs))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   328
        |> declare
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   329
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   330
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   331
fun get_const thy const =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   332
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   333
    val tabs = ConstName.get thy;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   334
    fun declare name =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   335
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   336
        val names' = (Consttab.update (const, name) (fst tabs),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   337
          Symtab.update_new (name, const) (snd tabs))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   338
      in (ConstName.change thy (K names'); name) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   339
  in case Consttab.lookup (fst tabs) const
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   340
   of SOME name => name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   341
    | NONE => 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   342
        const
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   343
        |> const_policy thy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   344
        |> Name.variant (Symtab.keys (snd tabs))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   345
        |> declare
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   346
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   347
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   348
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   349
(* backward lookup *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   350
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   351
fun rev thy get_tabs name =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   352
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   353
    val names_ref = CodeName.get thy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   354
    val (Names names) = ! names_ref;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   355
    val tab = (snd o get_tabs) names;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   356
  in case Symtab.lookup tab name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   357
   of SOME x => x
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   358
    | NONE => error ("No such " ^ category_of name ^ ": " ^ quote name)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   359
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   360
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   361
fun rev_const thy name =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   362
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   363
    val tab = snd (ConstName.get thy);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   364
  in case Symtab.lookup tab name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   365
   of SOME const => const
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   366
    | NONE => error ("No such " ^ category_of name ^ ": " ^ quote name)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   367
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   368
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   369
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   370
(* external interfaces *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   371
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   372
fun class thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   373
  get thy #class Symtab.lookup map_class Symtab.update class_policy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   374
  #> add_idf idf_class;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   375
fun classrel thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   376
  get thy #classrel StringPairTab.lookup map_classrel StringPairTab.update classrel_policy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   377
  #> add_idf idf_classrel;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   378
fun tyco thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   379
  get thy #tyco Symtab.lookup map_tyco Symtab.update tyco_policy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   380
  #> add_idf idf_tyco;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   381
fun instance thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   382
  get thy #instance StringPairTab.lookup map_instance StringPairTab.update instance_policy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   383
  #> add_idf idf_instance;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   384
fun const thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   385
  get_const thy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   386
  #> add_idf idf_const;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   387
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   388
fun class_rev thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   389
  dest_idf idf_class
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   390
  #> Option.map (rev thy #class);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   391
fun classrel_rev thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   392
  dest_idf idf_classrel
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   393
  #> Option.map (rev thy #classrel);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   394
fun tyco_rev thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   395
  dest_idf idf_tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   396
  #> Option.map (rev thy #tyco);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   397
fun instance_rev thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   398
  dest_idf idf_instance
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   399
  #> Option.map (rev thy #instance);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   400
fun const_rev thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   401
  dest_idf idf_const
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   402
  #> Option.map (rev_const thy);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   403
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   404
local
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   405
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   406
val f_mapping = [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   407
  (idf_class,       class_rev),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   408
  (idf_classrel,    Option.map string_of_classrel oo classrel_rev),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   409
  (idf_tyco,        tyco_rev),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   410
  (idf_instance,    Option.map string_of_instance oo instance_rev),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   411
  (idf_const,       fn thy => Option.map (CodeUnit.string_of_const thy) o const_rev thy)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   412
];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   413
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   414
in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   415
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   416
fun labelled_name thy idf =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   417
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   418
    val category = category_of idf;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   419
    val name = NameSpace.qualifier idf;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   420
    val f = (the o AList.lookup (op =) f_mapping o NameSpace.base) idf
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   421
  in case f thy idf
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   422
   of SOME thing => category ^ " " ^ quote thing
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   423
    | NONE => error ("Unknown name: " ^ quote name)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   424
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   425
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   426
end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   427
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   428
end;