src/Pure/name.ML
author haftmann
Thu Dec 04 14:43:33 2008 +0100 (2008-12-04)
changeset 28965 1de908189869
parent 28941 128459bd72d2
child 29006 abe0f11cfa4e
permissions -rw-r--r--
cleaned up binding module and related code
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@20089
    11
  val bound: int -> string
wenzelm@20089
    12
  val is_bound: string -> bool
wenzelm@20089
    13
  val internal: string -> string
wenzelm@20089
    14
  val dest_internal: string -> string
wenzelm@20089
    15
  val skolem: string -> string
wenzelm@20089
    16
  val dest_skolem: string -> string
wenzelm@20089
    17
  val clean_index: string * int -> string * int
wenzelm@20089
    18
  val clean: string -> string
wenzelm@20089
    19
  type context
wenzelm@20089
    20
  val context: context
wenzelm@20158
    21
  val make_context: string list -> context
wenzelm@20089
    22
  val declare: string -> context -> context
wenzelm@20158
    23
  val is_declared: context -> string -> bool
wenzelm@20089
    24
  val invents: context -> string -> int -> string list
haftmann@20192
    25
  val names: context -> string -> 'a list -> (string * 'a) list
wenzelm@20089
    26
  val invent_list: string list -> string -> int -> string list
wenzelm@20089
    27
  val variants: string list -> context -> string list * context
wenzelm@20089
    28
  val variant_list: string list -> string list -> string list
wenzelm@20089
    29
  val variant: string list -> string -> string
haftmann@28860
    30
haftmann@28941
    31
  val name_of: Binding.T -> string (*FIMXE legacy*)
wenzelm@20089
    32
end;
wenzelm@20089
    33
wenzelm@20089
    34
structure Name: NAME =
wenzelm@20089
    35
struct
wenzelm@20089
    36
wenzelm@24849
    37
(** common defaults **)
wenzelm@24849
    38
wenzelm@24849
    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@20089
    99
(* invents *)
wenzelm@20089
   100
wenzelm@20089
   101
fun invents ctxt =
wenzelm@20089
   102
  let
wenzelm@20089
   103
    fun invs _ 0 = []
wenzelm@20089
   104
      | invs x n =
wenzelm@20089
   105
          let val x' = Symbol.bump_string x in
wenzelm@20158
   106
            if is_declared ctxt x then invs x' n
wenzelm@20089
   107
            else x :: invs x' (n - 1)
wenzelm@20089
   108
          end;
wenzelm@20099
   109
  in invs o clean end;
wenzelm@20089
   110
wenzelm@20198
   111
fun names ctxt x xs = invents ctxt x (length xs) ~~ xs;
wenzelm@20198
   112
wenzelm@20089
   113
val invent_list = invents o make_context;
wenzelm@20089
   114
wenzelm@20089
   115
wenzelm@20089
   116
(* variants *)
wenzelm@20089
   117
wenzelm@20089
   118
(*makes a variant of a name distinct from already used names in a
wenzelm@20089
   119
  context; preserves a suffix of underscores "_"*)
wenzelm@20089
   120
val variants = fold_map (fn name => fn ctxt =>
wenzelm@20089
   121
  let
wenzelm@20089
   122
    fun vary x =
wenzelm@20089
   123
      (case declared ctxt x of
wenzelm@20089
   124
        NONE => x
wenzelm@20089
   125
      | SOME x' => vary (Symbol.bump_string (the_default x x')));
wenzelm@20089
   126
wenzelm@20121
   127
    val (x, n) = clean_index (name, 0);
wenzelm@20089
   128
    val (x', ctxt') =
wenzelm@20158
   129
      if not (is_declared ctxt x) then (x, declare x ctxt)
wenzelm@20089
   130
      else
wenzelm@20089
   131
        let
wenzelm@20089
   132
          val x0 = Symbol.bump_init x;
wenzelm@20089
   133
          val x' = vary x0;
wenzelm@20089
   134
          val ctxt' = ctxt
wenzelm@21565
   135
            |> x0 <> x' ? declare_renaming (x0, x')
wenzelm@20089
   136
            |> declare x';
wenzelm@20089
   137
        in (x', ctxt') end;
wenzelm@20089
   138
  in (x' ^ replicate_string n "_", ctxt') end);
wenzelm@20089
   139
wenzelm@20089
   140
fun variant_list used names = #1 (make_context used |> variants names);
wenzelm@20089
   141
fun variant used = singleton (variant_list used);
wenzelm@20089
   142
wenzelm@28075
   143
haftmann@28965
   144
(** generic name bindings -- legacy **)
haftmann@28941
   145
haftmann@28965
   146
val name_of = snd o Binding.dest;
wenzelm@28075
   147
wenzelm@20089
   148
end;