src/Pure/name.ML
author wenzelm
Sun, 30 Nov 2008 14:43:29 +0100
changeset 28917 20f43e0e0958
parent 28863 32e83a854e5e
child 28941 128459bd72d2
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/name.ML
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
     4
28075
a45ce1bae4e0 added type binding -- generic name bindings;
wenzelm
parents: 24849
diff changeset
     5
Names of basic logical entities (variables etc.).  Generic name bindings.
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
     6
*)
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
     7
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
     8
signature NAME =
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
     9
sig
24849
193a10e6bf90 added uu, aT;
wenzelm
parents: 21565
diff changeset
    10
  val uu: string
193a10e6bf90 added uu, aT;
wenzelm
parents: 21565
diff changeset
    11
  val aT: string
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    12
  val bound: int -> string
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    13
  val is_bound: string -> bool
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    14
  val internal: string -> string
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    15
  val dest_internal: string -> string
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    16
  val skolem: string -> string
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    17
  val dest_skolem: string -> string
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    18
  val clean_index: string * int -> string * int
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    19
  val clean: string -> string
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    20
  type context
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    21
  val context: context
20158
b71f4f4c6b1e export make_context, is_declared;
wenzelm
parents: 20121
diff changeset
    22
  val make_context: string list -> context
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    23
  val declare: string -> context -> context
20158
b71f4f4c6b1e export make_context, is_declared;
wenzelm
parents: 20121
diff changeset
    24
  val is_declared: context -> string -> bool
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    25
  val invents: context -> string -> int -> string list
20192
956cd30ef3be renamed Name.give_names to Name.names and moved Name.alphanum to Symbol.alphanum
haftmann
parents: 20174
diff changeset
    26
  val names: context -> string -> 'a list -> (string * 'a) list
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    27
  val invent_list: string list -> string -> int -> string list
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    28
  val variants: string list -> context -> string list * context
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    29
  val variant_list: string list -> string list -> string list
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    30
  val variant: string list -> string -> string
28860
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
    31
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
    32
  include NAME_BINDING
28725
4a71cc58b203 explicit interpretation prefix in Name.binding
haftmann
parents: 28108
diff changeset
    33
  val add_prefix: bool -> string -> binding -> binding
28821
78a6ed46ad04 Name.name_with_prefix (temporarily)
haftmann
parents: 28792
diff changeset
    34
  val map_prefix: ((string * bool) list -> binding -> binding) -> binding -> binding
28860
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
    35
  val name_of: binding -> string (*FIMXE legacy*)
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
    36
  val map_name: (string -> string) -> binding -> binding (*FIMXE legacy*)
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
    37
  val qualified: string -> binding -> binding (*FIMXE legacy*)
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
    38
  val display: binding -> string
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    39
end;
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    40
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    41
structure Name: NAME =
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    42
struct
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    43
28860
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
    44
open NameSpace;
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
    45
24849
193a10e6bf90 added uu, aT;
wenzelm
parents: 21565
diff changeset
    46
(** common defaults **)
193a10e6bf90 added uu, aT;
wenzelm
parents: 21565
diff changeset
    47
193a10e6bf90 added uu, aT;
wenzelm
parents: 21565
diff changeset
    48
val uu = "uu";
193a10e6bf90 added uu, aT;
wenzelm
parents: 21565
diff changeset
    49
val aT = "'a";
193a10e6bf90 added uu, aT;
wenzelm
parents: 21565
diff changeset
    50
193a10e6bf90 added uu, aT;
wenzelm
parents: 21565
diff changeset
    51
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    52
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    53
(** special variable names **)
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    54
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    55
(* encoded bounds *)
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    56
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    57
(*names for numbered variables --
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    58
  preserves order wrt. int_ord vs. string_ord, avoids allocating new strings*)
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    59
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    60
val small_int = Vector.tabulate (1000, fn i =>
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    61
  let val leading = if i < 10 then "00" else if i < 100 then "0" else ""
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    62
  in ":" ^ leading ^ string_of_int i end);
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    63
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    64
fun bound n =
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    65
  if n < 1000 then Vector.sub (small_int, n)
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    66
  else ":" ^ bound (n div 1000) ^ Vector.sub (small_int, n mod 1000);
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    67
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    68
val is_bound = String.isPrefix ":";
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    69
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    70
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    71
(* internal names *)
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    72
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    73
val internal = suffix "_";
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    74
val dest_internal = unsuffix "_";
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    75
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    76
val skolem = suffix "__";
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    77
val dest_skolem = unsuffix "__";
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    78
20099
bc3f9cb33645 clean: no special treatment of empty name;
wenzelm
parents: 20089
diff changeset
    79
fun clean_index (x, i) =
bc3f9cb33645 clean: no special treatment of empty name;
wenzelm
parents: 20089
diff changeset
    80
  (case try dest_internal x of
bc3f9cb33645 clean: no special treatment of empty name;
wenzelm
parents: 20089
diff changeset
    81
    NONE => (x, i)
bc3f9cb33645 clean: no special treatment of empty name;
wenzelm
parents: 20089
diff changeset
    82
  | SOME x' => clean_index (x', i + 1));
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    83
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    84
fun clean x = #1 (clean_index (x, 0));
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    85
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    86
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    87
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    88
(** generating fresh names **)
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    89
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    90
(* context *)
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    91
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    92
datatype context =
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    93
  Context of string option Symtab.table;    (*declared names with latest renaming*)
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    94
20099
bc3f9cb33645 clean: no special treatment of empty name;
wenzelm
parents: 20089
diff changeset
    95
fun declare x (Context tab) =
bc3f9cb33645 clean: no special treatment of empty name;
wenzelm
parents: 20089
diff changeset
    96
  Context (Symtab.default (clean x, NONE) tab);
bc3f9cb33645 clean: no special treatment of empty name;
wenzelm
parents: 20089
diff changeset
    97
bc3f9cb33645 clean: no special treatment of empty name;
wenzelm
parents: 20089
diff changeset
    98
fun declare_renaming (x, x') (Context tab) =
bc3f9cb33645 clean: no special treatment of empty name;
wenzelm
parents: 20089
diff changeset
    99
  Context (Symtab.update (clean x, SOME (clean x')) tab);
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   100
20158
b71f4f4c6b1e export make_context, is_declared;
wenzelm
parents: 20121
diff changeset
   101
fun is_declared (Context tab) = Symtab.defined tab;
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   102
fun declared (Context tab) = Symtab.lookup tab;
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   103
20121
848fc1a1d355 do not export make_context;
wenzelm
parents: 20104
diff changeset
   104
val context = Context Symtab.empty |> fold declare ["", "'"];
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   105
fun make_context used = fold declare used context;
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   106
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   107
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   108
(* invents *)
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   109
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   110
fun invents ctxt =
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   111
  let
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   112
    fun invs _ 0 = []
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   113
      | invs x n =
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   114
          let val x' = Symbol.bump_string x in
20158
b71f4f4c6b1e export make_context, is_declared;
wenzelm
parents: 20121
diff changeset
   115
            if is_declared ctxt x then invs x' n
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   116
            else x :: invs x' (n - 1)
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   117
          end;
20099
bc3f9cb33645 clean: no special treatment of empty name;
wenzelm
parents: 20089
diff changeset
   118
  in invs o clean end;
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   119
20198
wenzelm
parents: 20192
diff changeset
   120
fun names ctxt x xs = invents ctxt x (length xs) ~~ xs;
wenzelm
parents: 20192
diff changeset
   121
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   122
val invent_list = invents o make_context;
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   123
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   124
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   125
(* variants *)
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   126
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   127
(*makes a variant of a name distinct from already used names in a
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   128
  context; preserves a suffix of underscores "_"*)
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   129
val variants = fold_map (fn name => fn ctxt =>
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   130
  let
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   131
    fun vary x =
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   132
      (case declared ctxt x of
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   133
        NONE => x
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   134
      | SOME x' => vary (Symbol.bump_string (the_default x x')));
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   135
20121
848fc1a1d355 do not export make_context;
wenzelm
parents: 20104
diff changeset
   136
    val (x, n) = clean_index (name, 0);
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   137
    val (x', ctxt') =
20158
b71f4f4c6b1e export make_context, is_declared;
wenzelm
parents: 20121
diff changeset
   138
      if not (is_declared ctxt x) then (x, declare x ctxt)
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   139
      else
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   140
        let
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   141
          val x0 = Symbol.bump_init x;
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   142
          val x' = vary x0;
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   143
          val ctxt' = ctxt
21565
bd28361f4c5b simplified '?' operator;
wenzelm
parents: 20198
diff changeset
   144
            |> x0 <> x' ? declare_renaming (x0, x')
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   145
            |> declare x';
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   146
        in (x', ctxt') end;
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   147
  in (x' ^ replicate_string n "_", ctxt') end);
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   148
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   149
fun variant_list used names = #1 (make_context used |> variants names);
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   150
fun variant used = singleton (variant_list used);
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   151
28075
a45ce1bae4e0 added type binding -- generic name bindings;
wenzelm
parents: 24849
diff changeset
   152
a45ce1bae4e0 added type binding -- generic name bindings;
wenzelm
parents: 24849
diff changeset
   153
(** generic name bindings **)
a45ce1bae4e0 added type binding -- generic name bindings;
wenzelm
parents: 24849
diff changeset
   154
28860
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
   155
fun add_prefix sticky prfx b = if prfx = "" then error "empty name prefix"
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
   156
  else (map_binding o apfst) (cons (prfx, sticky)) b;
28075
a45ce1bae4e0 added type binding -- generic name bindings;
wenzelm
parents: 24849
diff changeset
   157
28860
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
   158
val prefix_of = fst o dest_binding;
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
   159
val name_of = snd o dest_binding;
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
   160
val map_name = map_binding o apsnd;
28108
1b08ed83b79e added qualified: string -> binding -> binding;
wenzelm
parents: 28075
diff changeset
   161
val qualified = map_name o NameSpace.qualified;
28075
a45ce1bae4e0 added type binding -- generic name bindings;
wenzelm
parents: 24849
diff changeset
   162
28860
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
   163
fun map_prefix f b =
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
   164
  let
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
   165
    val (prefix, name) = dest_binding b;
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
   166
    val pos = pos_of b;
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
   167
  in f prefix (binding_pos (name, pos)) end;
28792
1d80cee865de namify and name_decl combinators
haftmann
parents: 28737
diff changeset
   168
28860
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
   169
fun display b =
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
   170
  let
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
   171
    val (prefix, name) = dest_binding b
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
   172
    fun mk_prefix (prfx, true) = prfx
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
   173
      | mk_prefix (prfx, false) = enclose "(" ")" prfx
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
   174
  in if not (! NameSpace.long_names) orelse null prefix orelse name = "" then name
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
   175
    else NameSpace.implode (map mk_prefix prefix) ^ ":" ^ name
b1d46059d502 name spaces and name bindings
haftmann
parents: 28821
diff changeset
   176
  end;
28737
8cbb7cfcfb5b diagnostic output for name bindings
haftmann
parents: 28725
diff changeset
   177
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   178
end;