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