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