src/Pure/name.ML
author wenzelm
Tue Jul 11 12:17:17 2006 +0200 (2006-07-11)
changeset 20089 d757ebadb0a2
child 20099 bc3f9cb33645
permissions -rw-r--r--
Names of basic logical entities (variables etc.).
wenzelm@20089
     1
(*  Title:      Pure/name.ML
wenzelm@20089
     2
    ID:         $Id$
wenzelm@20089
     3
    Author:     Makarius
wenzelm@20089
     4
wenzelm@20089
     5
Names of basic logical entities (variables etc.).
wenzelm@20089
     6
*)
wenzelm@20089
     7
wenzelm@20089
     8
signature NAME =
wenzelm@20089
     9
sig
wenzelm@20089
    10
  val bound: int -> string
wenzelm@20089
    11
  val is_bound: string -> bool
wenzelm@20089
    12
  val internal: string -> string
wenzelm@20089
    13
  val dest_internal: string -> string
wenzelm@20089
    14
  val skolem: string -> string
wenzelm@20089
    15
  val dest_skolem: string -> string
wenzelm@20089
    16
  val clean_index: string * int -> string * int
wenzelm@20089
    17
  val clean: string -> string
wenzelm@20089
    18
  type context
wenzelm@20089
    19
  val context: context
wenzelm@20089
    20
  val make_context: string list -> context
wenzelm@20089
    21
  val declare: string -> context -> context
wenzelm@20089
    22
  val invents: context -> string -> int -> string list
wenzelm@20089
    23
  val invent_list: string list -> string -> int -> string list
wenzelm@20089
    24
  val variants: string list -> context -> string list * context
wenzelm@20089
    25
  val variant_list: string list -> string list -> string list
wenzelm@20089
    26
  val variant: string list -> string -> string
wenzelm@20089
    27
end;
wenzelm@20089
    28
wenzelm@20089
    29
structure Name: NAME =
wenzelm@20089
    30
struct
wenzelm@20089
    31
wenzelm@20089
    32
wenzelm@20089
    33
(** special variable names **)
wenzelm@20089
    34
wenzelm@20089
    35
(* encoded bounds *)
wenzelm@20089
    36
wenzelm@20089
    37
(*names for numbered variables --
wenzelm@20089
    38
  preserves order wrt. int_ord vs. string_ord, avoids allocating new strings*)
wenzelm@20089
    39
wenzelm@20089
    40
val small_int = Vector.tabulate (1000, fn i =>
wenzelm@20089
    41
  let val leading = if i < 10 then "00" else if i < 100 then "0" else ""
wenzelm@20089
    42
  in ":" ^ leading ^ string_of_int i end);
wenzelm@20089
    43
wenzelm@20089
    44
fun bound n =
wenzelm@20089
    45
  if n < 1000 then Vector.sub (small_int, n)
wenzelm@20089
    46
  else ":" ^ bound (n div 1000) ^ Vector.sub (small_int, n mod 1000);
wenzelm@20089
    47
wenzelm@20089
    48
val is_bound = String.isPrefix ":";
wenzelm@20089
    49
wenzelm@20089
    50
wenzelm@20089
    51
(* internal names *)
wenzelm@20089
    52
wenzelm@20089
    53
val internal = suffix "_";
wenzelm@20089
    54
val dest_internal = unsuffix "_";
wenzelm@20089
    55
wenzelm@20089
    56
val skolem = suffix "__";
wenzelm@20089
    57
val dest_skolem = unsuffix "__";
wenzelm@20089
    58
wenzelm@20089
    59
fun clean_index ("", i) = ("u", i + 1)
wenzelm@20089
    60
  | clean_index (x, i) =
wenzelm@20089
    61
      (case try dest_internal x of
wenzelm@20089
    62
        NONE => (x, i)
wenzelm@20089
    63
      | SOME x' => clean_index (x', i + 1));
wenzelm@20089
    64
wenzelm@20089
    65
fun clean x = #1 (clean_index (x, 0));
wenzelm@20089
    66
wenzelm@20089
    67
wenzelm@20089
    68
wenzelm@20089
    69
(** generating fresh names **)
wenzelm@20089
    70
wenzelm@20089
    71
(* context *)
wenzelm@20089
    72
wenzelm@20089
    73
datatype context =
wenzelm@20089
    74
  Context of string option Symtab.table;    (*declared names with latest renaming*)
wenzelm@20089
    75
wenzelm@20089
    76
fun declare x (Context tab) = Context (Symtab.default (x, NONE) tab);
wenzelm@20089
    77
fun declare_renaming (x, x') (Context tab) = Context (Symtab.update (x, SOME x') tab);
wenzelm@20089
    78
wenzelm@20089
    79
fun declared (Context tab) = Symtab.lookup tab;
wenzelm@20089
    80
wenzelm@20089
    81
val context = Context Symtab.empty;
wenzelm@20089
    82
fun make_context used = fold declare used context;
wenzelm@20089
    83
wenzelm@20089
    84
wenzelm@20089
    85
(* invents *)
wenzelm@20089
    86
wenzelm@20089
    87
fun invents ctxt =
wenzelm@20089
    88
  let
wenzelm@20089
    89
    fun invs _ 0 = []
wenzelm@20089
    90
      | invs x n =
wenzelm@20089
    91
          let val x' = Symbol.bump_string x in
wenzelm@20089
    92
            if is_some (declared ctxt x) then invs x' n
wenzelm@20089
    93
            else x :: invs x' (n - 1)
wenzelm@20089
    94
          end;
wenzelm@20089
    95
  in invs end;
wenzelm@20089
    96
wenzelm@20089
    97
val invent_list = invents o make_context;
wenzelm@20089
    98
wenzelm@20089
    99
wenzelm@20089
   100
(* variants *)
wenzelm@20089
   101
wenzelm@20089
   102
(*makes a variant of a name distinct from already used names in a
wenzelm@20089
   103
  context; preserves a suffix of underscores "_"*)
wenzelm@20089
   104
val variants = fold_map (fn name => fn ctxt =>
wenzelm@20089
   105
  let
wenzelm@20089
   106
    fun vary x =
wenzelm@20089
   107
      (case declared ctxt x of
wenzelm@20089
   108
        NONE => x
wenzelm@20089
   109
      | SOME x' => vary (Symbol.bump_string (the_default x x')));
wenzelm@20089
   110
wenzelm@20089
   111
    val (x, n) = clean_index (name, 0);
wenzelm@20089
   112
    val (x', ctxt') =
wenzelm@20089
   113
      if is_none (declared ctxt x) then (x, declare x ctxt)
wenzelm@20089
   114
      else
wenzelm@20089
   115
        let
wenzelm@20089
   116
          val x0 = Symbol.bump_init x;
wenzelm@20089
   117
          val x' = vary x0;
wenzelm@20089
   118
          val ctxt' = ctxt
wenzelm@20089
   119
            |> K (x0 <> x') ? declare_renaming (x0, x')
wenzelm@20089
   120
            |> declare x';
wenzelm@20089
   121
        in (x', ctxt') end;
wenzelm@20089
   122
  in (x' ^ replicate_string n "_", ctxt') end);
wenzelm@20089
   123
wenzelm@20089
   124
fun variant_list used names = #1 (make_context used |> variants names);
wenzelm@20089
   125
fun variant used = singleton (variant_list used);
wenzelm@20089
   126
wenzelm@20089
   127
end;