src/Pure/name.ML
author wenzelm
Sun Nov 11 20:31:46 2012 +0100 (2012-11-11)
changeset 50081 9b92ee8dec98
parent 43683 b5d1873449fb
child 55948 bb21b380f65d
permissions -rw-r--r--
tuned;
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@43683
    10
  val uu_: string
wenzelm@24849
    11
  val aT: 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@43329
    25
  val invent: context -> string -> int -> string list
wenzelm@43329
    26
  val invent_names: context -> string -> 'a list -> (string * 'a) list
wenzelm@20089
    27
  val invent_list: string list -> string -> int -> string list
wenzelm@43326
    28
  val variant: string -> context -> string * context
wenzelm@20089
    29
  val variant_list: string list -> string list -> string list
haftmann@31035
    30
  val desymbolize: bool -> 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@43683
    39
val uu_ = "uu_";
wenzelm@24849
    40
val aT = "'a";
wenzelm@24849
    41
wenzelm@24849
    42
wenzelm@20089
    43
wenzelm@20089
    44
(** special variable names **)
wenzelm@20089
    45
wenzelm@20089
    46
(* encoded bounds *)
wenzelm@20089
    47
wenzelm@20089
    48
(*names for numbered variables --
wenzelm@20089
    49
  preserves order wrt. int_ord vs. string_ord, avoids allocating new strings*)
wenzelm@20089
    50
wenzelm@20089
    51
val small_int = Vector.tabulate (1000, fn i =>
wenzelm@20089
    52
  let val leading = if i < 10 then "00" else if i < 100 then "0" else ""
wenzelm@20089
    53
  in ":" ^ leading ^ string_of_int i end);
wenzelm@20089
    54
wenzelm@20089
    55
fun bound n =
wenzelm@20089
    56
  if n < 1000 then Vector.sub (small_int, n)
wenzelm@20089
    57
  else ":" ^ bound (n div 1000) ^ Vector.sub (small_int, n mod 1000);
wenzelm@20089
    58
wenzelm@20089
    59
val is_bound = String.isPrefix ":";
wenzelm@20089
    60
wenzelm@20089
    61
wenzelm@20089
    62
(* internal names *)
wenzelm@20089
    63
wenzelm@20089
    64
val internal = suffix "_";
wenzelm@20089
    65
val dest_internal = unsuffix "_";
wenzelm@20089
    66
wenzelm@20089
    67
val skolem = suffix "__";
wenzelm@20089
    68
val dest_skolem = unsuffix "__";
wenzelm@20089
    69
wenzelm@20099
    70
fun clean_index (x, i) =
wenzelm@20099
    71
  (case try dest_internal x of
wenzelm@20099
    72
    NONE => (x, i)
wenzelm@20099
    73
  | SOME x' => clean_index (x', i + 1));
wenzelm@20089
    74
wenzelm@20089
    75
fun clean x = #1 (clean_index (x, 0));
wenzelm@20089
    76
wenzelm@20089
    77
wenzelm@20089
    78
wenzelm@20089
    79
(** generating fresh names **)
wenzelm@20089
    80
wenzelm@20089
    81
(* context *)
wenzelm@20089
    82
wenzelm@20089
    83
datatype context =
wenzelm@20089
    84
  Context of string option Symtab.table;    (*declared names with latest renaming*)
wenzelm@20089
    85
wenzelm@20099
    86
fun declare x (Context tab) =
wenzelm@20099
    87
  Context (Symtab.default (clean x, NONE) tab);
wenzelm@20099
    88
wenzelm@20099
    89
fun declare_renaming (x, x') (Context tab) =
wenzelm@20099
    90
  Context (Symtab.update (clean x, SOME (clean x')) tab);
wenzelm@20089
    91
wenzelm@20158
    92
fun is_declared (Context tab) = Symtab.defined tab;
wenzelm@20089
    93
fun declared (Context tab) = Symtab.lookup tab;
wenzelm@20089
    94
wenzelm@20121
    95
val context = Context Symtab.empty |> fold declare ["", "'"];
wenzelm@20089
    96
fun make_context used = fold declare used context;
wenzelm@20089
    97
wenzelm@20089
    98
wenzelm@43329
    99
(* invent names *)
wenzelm@20089
   100
wenzelm@43329
   101
fun invent ctxt =
wenzelm@20089
   102
  let
wenzelm@20089
   103
    fun invs _ 0 = []
wenzelm@20089
   104
      | invs x n =
wenzelm@43329
   105
          let val x' = Symbol.bump_string x
wenzelm@43329
   106
          in if is_declared ctxt x then invs x' n else x :: invs x' (n - 1) end;
wenzelm@20099
   107
  in invs o clean end;
wenzelm@20089
   108
wenzelm@43329
   109
fun invent_names ctxt x xs = invent ctxt x (length xs) ~~ xs;
wenzelm@20198
   110
wenzelm@43329
   111
val invent_list = invent o make_context;
wenzelm@20089
   112
wenzelm@20089
   113
wenzelm@20089
   114
(* variants *)
wenzelm@20089
   115
wenzelm@20089
   116
(*makes a variant of a name distinct from already used names in a
wenzelm@20089
   117
  context; preserves a suffix of underscores "_"*)
wenzelm@43326
   118
fun variant name ctxt =
wenzelm@20089
   119
  let
wenzelm@20089
   120
    fun vary x =
wenzelm@20089
   121
      (case declared ctxt x of
wenzelm@20089
   122
        NONE => x
wenzelm@20089
   123
      | SOME x' => vary (Symbol.bump_string (the_default x x')));
wenzelm@20089
   124
wenzelm@20121
   125
    val (x, n) = clean_index (name, 0);
wenzelm@20089
   126
    val (x', ctxt') =
wenzelm@20158
   127
      if not (is_declared ctxt x) then (x, declare x ctxt)
wenzelm@20089
   128
      else
wenzelm@20089
   129
        let
wenzelm@20089
   130
          val x0 = Symbol.bump_init x;
wenzelm@20089
   131
          val x' = vary x0;
wenzelm@20089
   132
          val ctxt' = ctxt
wenzelm@21565
   133
            |> x0 <> x' ? declare_renaming (x0, x')
wenzelm@20089
   134
            |> declare x';
wenzelm@20089
   135
        in (x', ctxt') end;
wenzelm@43326
   136
  in (x' ^ replicate_string n "_", ctxt') end;
wenzelm@20089
   137
wenzelm@43326
   138
fun variant_list used names = #1 (make_context used |> fold_map variant names);
wenzelm@20089
   139
haftmann@31013
   140
haftmann@31035
   141
(* names conforming to typical requirements of identifiers in the world outside *)
haftmann@31013
   142
haftmann@31035
   143
fun desymbolize upper "" = if upper then "X" else "x"
haftmann@31035
   144
  | desymbolize upper s =
haftmann@31013
   145
      let
haftmann@31035
   146
        val xs as (x :: _) = Symbol.explode s;
wenzelm@33547
   147
        val ys =
wenzelm@33547
   148
          if Symbol.is_ascii_letter x orelse Symbol.is_symbolic x then xs
haftmann@31013
   149
          else "x" :: xs;
haftmann@31013
   150
        fun is_valid x =
haftmann@34307
   151
          Symbol.is_ascii_letter x orelse Symbol.is_ascii_digit x;
haftmann@31013
   152
        fun sep [] = []
haftmann@31013
   153
          | sep (xs as "_" :: _) = xs
haftmann@31013
   154
          | sep xs = "_" :: xs;
haftmann@31035
   155
        fun desep ("_" :: xs) = xs
haftmann@31035
   156
          | desep xs = xs;
wenzelm@33547
   157
        fun desymb x xs =
wenzelm@33547
   158
          if is_valid x then x :: xs
haftmann@31013
   159
          else
wenzelm@33547
   160
            (case Symbol.decode x of
wenzelm@40627
   161
              Symbol.Sym name => "_" :: raw_explode name @ sep xs
wenzelm@33547
   162
            | _ => sep xs);
wenzelm@33547
   163
        fun upper_lower cs =
wenzelm@33547
   164
          if upper then nth_map 0 Symbol.to_ascii_upper cs
wenzelm@33547
   165
          else
wenzelm@33547
   166
            (if forall Symbol.is_ascii_upper cs then map else nth_map 0)
wenzelm@33547
   167
              Symbol.to_ascii_lower cs;
haftmann@31035
   168
      in fold_rev desymb ys [] |> desep |> upper_lower |> implode end;
haftmann@31013
   169
wenzelm@20089
   170
end;