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