src/Pure/name.ML
author wenzelm
Thu Mar 19 13:26:19 2009 +0100 (2009-03-19)
changeset 30584 7e839627b9e7
parent 29006 abe0f11cfa4e
child 31013 69a476d6fea6
permissions -rw-r--r--
Name.of_binding: proper full_name (with checks) before projecting base name;
wenzelm@20089
     1
(*  Title:      Pure/name.ML
wenzelm@20089
     2
    Author:     Makarius
wenzelm@20089
     3
haftmann@28965
     4
Names of basic logical entities (variables etc.).
wenzelm@20089
     5
*)
wenzelm@20089
     6
wenzelm@20089
     7
signature NAME =
wenzelm@20089
     8
sig
wenzelm@24849
     9
  val uu: string
wenzelm@24849
    10
  val aT: string
wenzelm@30584
    11
  val of_binding: binding -> string
wenzelm@20089
    12
  val bound: int -> string
wenzelm@20089
    13
  val is_bound: string -> bool
wenzelm@20089
    14
  val internal: string -> string
wenzelm@20089
    15
  val dest_internal: string -> string
wenzelm@20089
    16
  val skolem: string -> string
wenzelm@20089
    17
  val dest_skolem: string -> string
wenzelm@20089
    18
  val clean_index: string * int -> string * int
wenzelm@20089
    19
  val clean: string -> string
wenzelm@20089
    20
  type context
wenzelm@20089
    21
  val context: context
wenzelm@20158
    22
  val make_context: string list -> context
wenzelm@20089
    23
  val declare: string -> context -> context
wenzelm@20158
    24
  val is_declared: context -> string -> bool
wenzelm@20089
    25
  val invents: context -> string -> int -> string list
haftmann@20192
    26
  val names: context -> string -> 'a list -> (string * 'a) list
wenzelm@20089
    27
  val invent_list: string list -> string -> int -> string list
wenzelm@20089
    28
  val variants: string list -> context -> string list * context
wenzelm@20089
    29
  val variant_list: string list -> string list -> string list
wenzelm@20089
    30
  val variant: string list -> string -> string
wenzelm@20089
    31
end;
wenzelm@20089
    32
wenzelm@20089
    33
structure Name: NAME =
wenzelm@20089
    34
struct
wenzelm@20089
    35
wenzelm@24849
    36
(** common defaults **)
wenzelm@24849
    37
wenzelm@24849
    38
val uu = "uu";
wenzelm@24849
    39
val aT = "'a";
wenzelm@24849
    40
wenzelm@24849
    41
wenzelm@20089
    42
wenzelm@20089
    43
(** special variable names **)
wenzelm@20089
    44
wenzelm@30584
    45
(* checked binding *)
wenzelm@30584
    46
wenzelm@30584
    47
val of_binding = Long_Name.base_name o NameSpace.full_name NameSpace.default_naming;
wenzelm@30584
    48
wenzelm@30584
    49
wenzelm@20089
    50
(* encoded bounds *)
wenzelm@20089
    51
wenzelm@20089
    52
(*names for numbered variables --
wenzelm@20089
    53
  preserves order wrt. int_ord vs. string_ord, avoids allocating new strings*)
wenzelm@20089
    54
wenzelm@20089
    55
val small_int = Vector.tabulate (1000, fn i =>
wenzelm@20089
    56
  let val leading = if i < 10 then "00" else if i < 100 then "0" else ""
wenzelm@20089
    57
  in ":" ^ leading ^ string_of_int i end);
wenzelm@20089
    58
wenzelm@20089
    59
fun bound n =
wenzelm@20089
    60
  if n < 1000 then Vector.sub (small_int, n)
wenzelm@20089
    61
  else ":" ^ bound (n div 1000) ^ Vector.sub (small_int, n mod 1000);
wenzelm@20089
    62
wenzelm@20089
    63
val is_bound = String.isPrefix ":";
wenzelm@20089
    64
wenzelm@20089
    65
wenzelm@20089
    66
(* internal names *)
wenzelm@20089
    67
wenzelm@20089
    68
val internal = suffix "_";
wenzelm@20089
    69
val dest_internal = unsuffix "_";
wenzelm@20089
    70
wenzelm@20089
    71
val skolem = suffix "__";
wenzelm@20089
    72
val dest_skolem = unsuffix "__";
wenzelm@20089
    73
wenzelm@20099
    74
fun clean_index (x, i) =
wenzelm@20099
    75
  (case try dest_internal x of
wenzelm@20099
    76
    NONE => (x, i)
wenzelm@20099
    77
  | SOME x' => clean_index (x', i + 1));
wenzelm@20089
    78
wenzelm@20089
    79
fun clean x = #1 (clean_index (x, 0));
wenzelm@20089
    80
wenzelm@20089
    81
wenzelm@20089
    82
wenzelm@20089
    83
(** generating fresh names **)
wenzelm@20089
    84
wenzelm@20089
    85
(* context *)
wenzelm@20089
    86
wenzelm@20089
    87
datatype context =
wenzelm@20089
    88
  Context of string option Symtab.table;    (*declared names with latest renaming*)
wenzelm@20089
    89
wenzelm@20099
    90
fun declare x (Context tab) =
wenzelm@20099
    91
  Context (Symtab.default (clean x, NONE) tab);
wenzelm@20099
    92
wenzelm@20099
    93
fun declare_renaming (x, x') (Context tab) =
wenzelm@20099
    94
  Context (Symtab.update (clean x, SOME (clean x')) tab);
wenzelm@20089
    95
wenzelm@20158
    96
fun is_declared (Context tab) = Symtab.defined tab;
wenzelm@20089
    97
fun declared (Context tab) = Symtab.lookup tab;
wenzelm@20089
    98
wenzelm@20121
    99
val context = Context Symtab.empty |> fold declare ["", "'"];
wenzelm@20089
   100
fun make_context used = fold declare used context;
wenzelm@20089
   101
wenzelm@20089
   102
wenzelm@20089
   103
(* invents *)
wenzelm@20089
   104
wenzelm@20089
   105
fun invents ctxt =
wenzelm@20089
   106
  let
wenzelm@20089
   107
    fun invs _ 0 = []
wenzelm@20089
   108
      | invs x n =
wenzelm@20089
   109
          let val x' = Symbol.bump_string x in
wenzelm@20158
   110
            if is_declared ctxt x then invs x' n
wenzelm@20089
   111
            else x :: invs x' (n - 1)
wenzelm@20089
   112
          end;
wenzelm@20099
   113
  in invs o clean end;
wenzelm@20089
   114
wenzelm@20198
   115
fun names ctxt x xs = invents ctxt x (length xs) ~~ xs;
wenzelm@20198
   116
wenzelm@20089
   117
val invent_list = invents o make_context;
wenzelm@20089
   118
wenzelm@20089
   119
wenzelm@20089
   120
(* variants *)
wenzelm@20089
   121
wenzelm@20089
   122
(*makes a variant of a name distinct from already used names in a
wenzelm@20089
   123
  context; preserves a suffix of underscores "_"*)
wenzelm@20089
   124
val variants = fold_map (fn name => fn ctxt =>
wenzelm@20089
   125
  let
wenzelm@20089
   126
    fun vary x =
wenzelm@20089
   127
      (case declared ctxt x of
wenzelm@20089
   128
        NONE => x
wenzelm@20089
   129
      | SOME x' => vary (Symbol.bump_string (the_default x x')));
wenzelm@20089
   130
wenzelm@20121
   131
    val (x, n) = clean_index (name, 0);
wenzelm@20089
   132
    val (x', ctxt') =
wenzelm@20158
   133
      if not (is_declared ctxt x) then (x, declare x ctxt)
wenzelm@20089
   134
      else
wenzelm@20089
   135
        let
wenzelm@20089
   136
          val x0 = Symbol.bump_init x;
wenzelm@20089
   137
          val x' = vary x0;
wenzelm@20089
   138
          val ctxt' = ctxt
wenzelm@21565
   139
            |> x0 <> x' ? declare_renaming (x0, x')
wenzelm@20089
   140
            |> declare x';
wenzelm@20089
   141
        in (x', ctxt') end;
wenzelm@20089
   142
  in (x' ^ replicate_string n "_", ctxt') end);
wenzelm@20089
   143
wenzelm@20089
   144
fun variant_list used names = #1 (make_context used |> variants names);
wenzelm@20089
   145
fun variant used = singleton (variant_list used);
wenzelm@20089
   146
wenzelm@20089
   147
end;