src/Pure/Tools/codegen_names.ML
author haftmann
Tue, 09 Jan 2007 08:31:49 +0100
changeset 22034 44ab6c04b3dc
parent 21931 314f9e2a442c
child 22057 d7c91b2f5a9e
permissions -rw-r--r--
moved variable environments here
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,
21915
4e63c55f4cb4 different handling of type variable names
haftmann
parents: 21880
diff changeset
     6
conversion to alphanumeric representation, name space identifier.
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
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
    12
  type tyco = string
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
    13
  type const = string
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
    14
  val class: theory -> class -> class
20697
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
    15
  val class_rev: theory -> class -> class option
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
    16
  val classrel: theory -> class * class -> string
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
    17
  val classrel_rev: theory -> string -> (class * class) option
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
    18
  val tyco: theory -> tyco -> tyco
20697
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
    19
  val tyco_rev: theory -> tyco -> tyco option
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
    20
  val instance: theory -> class * tyco -> string
20697
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
    21
  val instance_rev: theory -> string -> (class * tyco) option
20585
3fe913d70177 cleanupdiff
haftmann
parents: 20456
diff changeset
    22
  val const: theory -> CodegenConsts.const -> const
20697
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
    23
  val const_rev: theory -> const -> CodegenConsts.const option
22034
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    24
  val labelled_name: theory -> string -> string
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    25
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    26
  val check_modulename: string -> string
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
    27
  val purify_var: string -> string
21915
4e63c55f4cb4 different handling of type variable names
haftmann
parents: 21880
diff changeset
    28
  val purify_tvar: string -> string
22034
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    29
  
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    30
  type var_ctxt;
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    31
  val make_vars: string list -> var_ctxt;
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    32
  val intro_vars: string list -> var_ctxt -> var_ctxt;
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    33
  val lookup_var: var_ctxt -> string -> string;
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
    34
end;
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
    35
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
    36
structure CodegenNames: CODEGEN_NAMES =
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
    37
struct
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
    38
22034
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    39
(** purification **)
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    40
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    41
val purify_name =
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    42
  let
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    43
    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
    44
    val is_junk = not o is_valid andf Symbol.not_eof;
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    45
    val junk = Scan.many is_junk;
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    46
    val scan_valids = Symbol.scanner "Malformed input"
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    47
      ((junk |--
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    48
        (Scan.optional (Scan.one Symbol.is_ascii_letter) "x" ^^ (Scan.many is_valid >> implode)
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    49
        --| junk))
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    50
      -- Scan.repeat ((Scan.many1 is_valid >> implode) --| junk) >> op ::);
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    51
  in explode #> scan_valids #> space_implode "_" end;
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    52
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    53
fun purify_op "0" = "zero"
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    54
  | purify_op "1" = "one"
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    55
  | purify_op s =
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    56
      let
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    57
        fun rep_op "+" = SOME "sum"
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    58
          | rep_op "-" = SOME "diff"
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    59
          | rep_op "*" = SOME "prod"
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    60
          | rep_op "/" = SOME "quotient"
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    61
          | rep_op "&" = SOME "conj"
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    62
          | rep_op "|" = SOME "disj"
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    63
          | rep_op "=" = SOME "eq"
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    64
          | rep_op "~" = SOME "inv"
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    65
          | rep_op "@" = SOME "append"
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
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    79
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
    80
(** global names (identifiers) **)
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
    81
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
    82
(* theory data *)
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
    83
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
    84
type tyco = string;
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
    85
type const = string;
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
    86
val string_pair_ord = prod_ord fast_string_ord fast_string_ord;
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
    87
val eq_string_pair = is_equal o string_pair_ord;
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
    88
structure Consttab = CodegenConsts.Consttab;
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
    89
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
    90
structure StringPairTab =
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
    91
  TableFun(
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
    92
    type key = string * string;
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
    93
    val ord = string_pair_ord;
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
    94
  );
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
    95
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
    96
datatype names = Names of {
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
    97
  class: class Symtab.table * class Symtab.table,
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
    98
  classrel: string StringPairTab.table * (class * class) Symtab.table,
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
    99
  tyco: tyco Symtab.table * tyco Symtab.table,
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   100
  instance: string StringPairTab.table * (class * tyco) Symtab.table,
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   101
  const: const Consttab.table * (string * typ list) Symtab.table
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   102
}
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   103
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   104
val empty_names = Names {
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   105
  class = (Symtab.empty, Symtab.empty),
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   106
  classrel = (StringPairTab.empty, Symtab.empty),
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   107
  tyco = (Symtab.empty, Symtab.empty),
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   108
  instance = (StringPairTab.empty, Symtab.empty),
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   109
  const = (Consttab.empty, Symtab.empty)
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   110
};
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   111
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   112
local
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   113
  fun mk_names (class, classrel, tyco, instance, const) =
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   114
    Names { class = class, classrel = classrel, tyco = tyco, instance = instance, const = const};
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   115
  fun map_names f (Names { class, classrel, tyco, instance, const }) =
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   116
    mk_names (f (class, classrel, tyco, instance, const));
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   117
  val eq_string = op = : string * string -> bool;
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   118
in
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   119
  fun merge_names (Names { class = (class1, classrev1),
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   120
      classrel = (classrel1, classrelrev1), tyco = (tyco1, tycorev1),
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   121
      instance = (instance1, instancerev1), const = (const1, constrev1) },
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   122
    Names { class = (class2, classrev2),
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   123
      classrel = (classrel2, classrelrev2), tyco = (tyco2, tycorev2),
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   124
      instance = (instance2, instancerev2), const = (const2, constrev2) }) =
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   125
    mk_names ((Symtab.merge eq_string (class1, class2), Symtab.merge eq_string (classrev1, classrev2)),
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   126
      (StringPairTab.merge eq_string (classrel1, classrel2), Symtab.merge eq_string_pair (classrelrev1, classrelrev2)),
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   127
      (Symtab.merge eq_string (tyco1, tyco2), Symtab.merge eq_string (tycorev1, tycorev2)),
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   128
      (StringPairTab.merge eq_string (instance1, instance2), Symtab.merge eq_string_pair (instancerev1, instancerev2)),
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   129
      (Consttab.merge eq_string (const1, const2), Symtab.merge CodegenConsts.eq_const (constrev1, constrev2)));
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   130
  fun map_class f = map_names
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   131
    (fn (class, classrel, tyco, inst, const) => (f class, classrel, tyco, inst, const));
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   132
  fun map_classrel f = map_names
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   133
    (fn (class, classrel, tyco, inst, const) => (class, f classrel, tyco, inst, const));
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   134
  fun map_tyco f = map_names
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   135
    (fn (class, classrel, tyco, inst, const) => (class, classrel, f tyco, inst, const));
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   136
  fun map_inst f = map_names
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   137
    (fn (class, classrel, tyco, inst, const) => (class, classrel, tyco, f inst, const));
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   138
  fun map_const f = map_names
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   139
    (fn (class, classrel, tyco, inst, const) => (class, classrel, tyco, inst, f const));
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   140
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   141
end; (*local*)
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   142
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20389
diff changeset
   143
structure CodeName = TheoryDataFun
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   144
(struct
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   145
  val name = "Pure/codegen_names";
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   146
  type T = names ref;
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   147
  val empty = ref empty_names;
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   148
  fun copy (ref names) = ref names;
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   149
  val extend = copy;
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   150
  fun merge _ (ref names1, ref names2) = ref (merge_names (names1, names2));
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   151
  fun print _ _ = ();
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   152
end);
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   153
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20389
diff changeset
   154
val _ = Context.add_setup CodeName.init;
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   155
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   156
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   157
(* forward lookup with cache update *)
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   158
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   159
fun get thy get_tabs get upd_names upd policy x =
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   160
  let
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20389
diff changeset
   161
    val names_ref = CodeName.get thy
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   162
    val (Names names) = ! names_ref;
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   163
    val tabs = get_tabs names;
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   164
    fun declare name =
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   165
      let
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   166
        val names' = upd_names (K (upd (x, name) (fst tabs),
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   167
          Symtab.update_new (name, x) (snd tabs))) (Names names)
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   168
      in (names_ref := names'; name) end;
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   169
  in case get (fst tabs) x
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   170
   of SOME name => name
21338
56d55dd30311 cleaned up
haftmann
parents: 21191
diff changeset
   171
    | NONE => 
56d55dd30311 cleaned up
haftmann
parents: 21191
diff changeset
   172
        x
56d55dd30311 cleaned up
haftmann
parents: 21191
diff changeset
   173
        |> policy thy
56d55dd30311 cleaned up
haftmann
parents: 21191
diff changeset
   174
        |> Name.variant (Symtab.keys (snd tabs))
56d55dd30311 cleaned up
haftmann
parents: 21191
diff changeset
   175
        |> declare
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   176
  end;
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   177
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   178
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   179
(* backward lookup *)
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   180
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   181
fun rev thy get_tabs errname name =
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   182
  let
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20389
diff changeset
   183
    val names_ref = CodeName.get thy
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   184
    val (Names names) = ! names_ref;
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   185
    val tab = (snd o get_tabs) names;
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   186
  in case Symtab.lookup tab name
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   187
   of SOME x => x
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   188
    | NONE => error ("No such " ^ errname ^ ": " ^ quote name)
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   189
  end;
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   190
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   191
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   192
(* theory name lookup *)
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   193
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   194
fun thyname_of thy f errmsg x =
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   195
  let
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   196
    fun thy_of thy =
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   197
      if f thy x then case get_first thy_of (Theory.parents_of thy)
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   198
        of NONE => SOME thy
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   199
         | thy => thy
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   200
      else NONE;
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   201
  in case thy_of thy
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   202
   of SOME thy => Context.theory_name thy
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   203
    | NONE => error (errmsg x) end;
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   204
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   205
fun thyname_of_class thy =
21931
314f9e2a442c replaced Sign.classes by Sign.all_classes (topologically sorted);
wenzelm
parents: 21927
diff changeset
   206
  thyname_of thy (fn thy => member (op =) (Sign.all_classes thy))
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   207
    (fn class => "thyname_of_class: no such class: " ^ quote class);
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   208
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   209
fun thyname_of_classrel thy =
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   210
  thyname_of thy (fn thy => fn (class1, class2) => Sign.subsort thy ([class1], [class2]))
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   211
    (fn (class1, class2) => "thyname_of_classrel: no such classrel: " ^ quote class1 ^ " in " ^ quote class2);
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   212
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   213
fun thyname_of_tyco thy =
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   214
  thyname_of thy Sign.declared_tyname
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   215
    (fn tyco => "thyname_of_tyco: no such type constructor: " ^ quote tyco);
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   216
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   217
fun thyname_of_instance thy =
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   218
  let
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   219
    fun test_instance thy (class, tyco) =
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   220
      can (Sorts.mg_domain (Sign.classes_of thy) tyco) [class]
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   221
  in thyname_of thy test_instance
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   222
    (fn (class, tyco) => "thyname_of_instance: no such instance: " ^ quote class ^ ", " ^ quote tyco)
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   223
  end;
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   224
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   225
fun thyname_of_const thy =
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   226
  thyname_of thy Sign.declared_const
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   227
    (fn c => "thyname_of_const: no such constant: " ^ quote c);
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   228
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   229
22034
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   230
(* naming policies *)
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   231
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   232
val purify_idf = purify_op #> purify_name;
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   233
val purify_prefix = map (purify_idf #> purify_upper);
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   234
val purify_base = purify_idf #> purify_lower;
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   235
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   236
val dotify =
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   237
  explode
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   238
  (*should disappear as soon as hierarchical theory name spaces are available*)
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   239
  #> Symbol.scanner "Malformed name"
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   240
      (Scan.repeat ($$ "_" |-- $$ "_" >> (fn _ => ".") || Scan.one Symbol.not_eof))
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   241
  #> implode;
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   242
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   243
fun policy thy get_basename get_thyname name =
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   244
  let
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   245
    val prefix = (purify_prefix o NameSpace.explode o dotify o get_thyname thy) name;
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   246
    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
   247
  in NameSpace.implode (prefix @ [base]) end;
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   248
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   249
fun class_policy thy = policy thy NameSpace.base thyname_of_class;
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   250
fun classrel_policy thy = policy thy (fn (class1, class2) => 
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   251
  NameSpace.base class2 ^ "_" ^ NameSpace.base class1) thyname_of_classrel;
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   252
fun tyco_policy thy = policy thy NameSpace.base thyname_of_tyco;
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   253
fun instance_policy thy = policy thy (fn (class, tyco) => 
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   254
  NameSpace.base class ^ "_" ^ NameSpace.base tyco) thyname_of_instance;
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   255
21461
51239d45247b forced name prefix for class operations
haftmann
parents: 21338
diff changeset
   256
fun force_thyname thy (const as (c, tys)) =
20585
3fe913d70177 cleanupdiff
haftmann
parents: 20456
diff changeset
   257
  case AxClass.class_of_param thy c
3fe913d70177 cleanupdiff
haftmann
parents: 20456
diff changeset
   258
   of SOME class => (case tys
3fe913d70177 cleanupdiff
haftmann
parents: 20456
diff changeset
   259
       of [Type (tyco, _)] => SOME (thyname_of_instance thy (class, tyco))
21461
51239d45247b forced name prefix for class operations
haftmann
parents: 21338
diff changeset
   260
        | _ => SOME (thyname_of_class thy class))
51239d45247b forced name prefix for class operations
haftmann
parents: 21338
diff changeset
   261
    | NONE => (case CodegenData.get_datatype_of_constr thy const
51239d45247b forced name prefix for class operations
haftmann
parents: 21338
diff changeset
   262
   of SOME dtco => SOME (thyname_of_tyco thy dtco)
51239d45247b forced name prefix for class operations
haftmann
parents: 21338
diff changeset
   263
    | NONE => (case CodegenConsts.find_def thy const
20585
3fe913d70177 cleanupdiff
haftmann
parents: 20456
diff changeset
   264
   of SOME ((thyname, _), _) => SOME thyname
21461
51239d45247b forced name prefix for class operations
haftmann
parents: 21338
diff changeset
   265
    | NONE => NONE));
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   266
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   267
fun const_policy thy (c, tys) =
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   268
  case force_thyname thy (c, tys)
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   269
   of NONE => policy thy NameSpace.base thyname_of_const c
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   270
    | SOME thyname => let
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   271
        val prefix = (purify_prefix o NameSpace.explode o dotify) thyname;
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   272
        val tycos = map_filter (fn Type (tyco, _) => SOME tyco | _ => NONE) tys;
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   273
        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
   274
      in NameSpace.implode (prefix @ [space_implode "_" base]) end;
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   275
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   276
20697
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
   277
(* shallow name spaces *)
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
   278
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
   279
val nsp_class = "class";
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   280
val nsp_classrel = "clsrel"
20697
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
   281
val nsp_tyco = "tyco";
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
   282
val nsp_inst = "inst";
21915
4e63c55f4cb4 different handling of type variable names
haftmann
parents: 21880
diff changeset
   283
val nsp_const = "const";
20697
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
   284
22034
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   285
val nsp_mapping = [
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   286
  (nsp_class,       "class"),
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   287
  (nsp_classrel,    "class relation"),
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   288
  (nsp_tyco,        "type constructor"),
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   289
  (nsp_inst,        "instance"),
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   290
  (nsp_const,       "constant")
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   291
]
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   292
21915
4e63c55f4cb4 different handling of type variable names
haftmann
parents: 21880
diff changeset
   293
fun add_nsp nsp name =
4e63c55f4cb4 different handling of type variable names
haftmann
parents: 21880
diff changeset
   294
  NameSpace.append name nsp
20697
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
   295
21915
4e63c55f4cb4 different handling of type variable names
haftmann
parents: 21880
diff changeset
   296
fun dest_nsp nsp name =
4e63c55f4cb4 different handling of type variable names
haftmann
parents: 21880
diff changeset
   297
  if NameSpace.base name = nsp
4e63c55f4cb4 different handling of type variable names
haftmann
parents: 21880
diff changeset
   298
  then SOME (NameSpace.qualifier name)
4e63c55f4cb4 different handling of type variable names
haftmann
parents: 21880
diff changeset
   299
  else NONE;
20697
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
   300
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
   301
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   302
(* external interfaces *)
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   303
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   304
fun class thy =
20697
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
   305
  get thy #class Symtab.lookup map_class Symtab.update class_policy
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
   306
  #> add_nsp nsp_class;
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   307
fun classrel thy =
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   308
  get thy #classrel StringPairTab.lookup map_classrel StringPairTab.update classrel_policy
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   309
  #> add_nsp nsp_classrel;
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   310
fun tyco thy =
20697
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
   311
  get thy #tyco Symtab.lookup map_tyco Symtab.update tyco_policy
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
   312
  #> add_nsp nsp_tyco;
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   313
fun instance thy =
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   314
  get thy #instance StringPairTab.lookup map_inst StringPairTab.update instance_policy
20697
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
   315
  #> add_nsp nsp_inst;
21915
4e63c55f4cb4 different handling of type variable names
haftmann
parents: 21880
diff changeset
   316
fun const thy =
4e63c55f4cb4 different handling of type variable names
haftmann
parents: 21880
diff changeset
   317
  CodegenConsts.norm thy
4e63c55f4cb4 different handling of type variable names
haftmann
parents: 21880
diff changeset
   318
  #> get thy #const Consttab.lookup map_const Consttab.update const_policy 
4e63c55f4cb4 different handling of type variable names
haftmann
parents: 21880
diff changeset
   319
  #> add_nsp nsp_const;
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   320
20697
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
   321
fun class_rev thy =
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
   322
  dest_nsp nsp_class
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
   323
  #> Option.map (rev thy #class "class");
21927
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   324
fun classrel_rev thy =
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   325
  dest_nsp nsp_classrel
9677abe5d374 added handling for explicit classrel witnesses
haftmann
parents: 21915
diff changeset
   326
  #> Option.map (rev thy #classrel "class relation");
20697
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
   327
fun tyco_rev thy =
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
   328
  dest_nsp nsp_tyco
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
   329
  #> Option.map (rev thy #tyco "type constructor");
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
   330
fun instance_rev thy =
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
   331
  dest_nsp nsp_inst
12952535fc2c added code_instname
haftmann
parents: 20585
diff changeset
   332
  #> Option.map (rev thy #instance "instance");
21915
4e63c55f4cb4 different handling of type variable names
haftmann
parents: 21880
diff changeset
   333
fun const_rev thy =
4e63c55f4cb4 different handling of type variable names
haftmann
parents: 21880
diff changeset
   334
  dest_nsp nsp_const
4e63c55f4cb4 different handling of type variable names
haftmann
parents: 21880
diff changeset
   335
  #> Option.map (rev thy #const "constant");
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   336
22034
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   337
fun labelled_name thy name =
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   338
  let
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   339
    val (base, nsp) = NameSpace.split name
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   340
  in case AList.lookup (op =) nsp_mapping nsp
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   341
   of SOME msg => msg ^ " " ^ quote base
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   342
    | NONE => error ("illegal shallow name space: " ^ quote nsp)
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   343
  end;
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   344
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   345
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   346
(** variable and module names **)
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   347
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   348
fun purify_var "" = "x"
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   349
  | purify_var v = (purify_name #> purify_lower) v;
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   350
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   351
fun purify_tvar "" = "'a"
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   352
  | purify_tvar v =
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   353
      (unprefix "'" #> explode #> filter Symbol.is_ascii_letter #> cons "'" #> implode) v;
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   354
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   355
fun check_modulename mn =
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   356
  let
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   357
    val mns = NameSpace.explode mn;
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   358
    val mns' = map purify_upper mns;
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   359
  in
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   360
    if mns' = mns then mn else error ("Invalid module name: " ^ quote mn ^ "\n"
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   361
      ^ "perhaps try " ^ quote (NameSpace.implode mns'))
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   362
  end;
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   363
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   364
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   365
(** variable name contexts **)
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   366
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   367
type var_ctxt = string Symtab.table * Name.context;
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   368
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   369
fun make_vars names = (fold (fn name => Symtab.update_new (name, name)) names Symtab.empty,
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   370
  Name.make_context names);
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   371
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   372
fun intro_vars names (namemap, namectxt) =
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   373
  let
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   374
    val (names', namectxt') = Name.variants names namectxt;
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   375
    val namemap' = fold2 (curry Symtab.update) names names' namemap;
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   376
  in (namemap', namectxt') end;
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   377
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   378
fun lookup_var (namemap, _) name = case Symtab.lookup namemap name
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   379
 of SOME name' => name'
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   380
  | NONE => error ("invalid name in context: " ^ quote name);
44ab6c04b3dc moved variable environments here
haftmann
parents: 21931
diff changeset
   381
20353
d73e49780ef2 code generator refinements
haftmann
parents:
diff changeset
   382
end;