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