src/Pure/name.ML
author wenzelm
Mon, 02 Dec 2024 22:16:29 +0100
changeset 81541 5335b1ca6233
parent 81521 1bfad73ab115
permissions -rw-r--r--
more elementary operation Term.variant_bounds: only for bounds vs. frees, no consts, no tfrees;
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
    Author:     Makarius
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
     3
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
     4
Names of basic logical entities (variables etc.).
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
     5
*)
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
signature NAME =
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
     8
sig
24849
193a10e6bf90 added uu, aT;
wenzelm
parents: 21565
diff changeset
     9
  val uu: string
43683
b5d1873449fb tuned errors;
wenzelm
parents: 43329
diff changeset
    10
  val uu_: string
24849
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
55948
bb21b380f65d tuned signature;
wenzelm
parents: 43683
diff changeset
    16
  val is_internal: string -> bool
55949
4766342e8376 clarified check of internal names;
wenzelm
parents: 55948
diff changeset
    17
  val reject_internal: string * Position.T list -> unit
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    18
  val skolem: string -> string
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    19
  val dest_skolem: string -> string
55948
bb21b380f65d tuned signature;
wenzelm
parents: 43683
diff changeset
    20
  val is_skolem: string -> bool
55949
4766342e8376 clarified check of internal names;
wenzelm
parents: 55948
diff changeset
    21
  val reject_skolem: string * Position.T list -> unit
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    22
  val clean_index: string * int -> string * int
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    23
  val clean: string -> string
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    24
  type context
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    25
  val context: context
74411
20b0b27bc6c7 clarified signature;
wenzelm
parents: 69137
diff changeset
    26
  val build_context: (context -> context) -> context
20158
b71f4f4c6b1e export make_context, is_declared;
wenzelm
parents: 20121
diff changeset
    27
  val make_context: string list -> context
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    28
  val declare: string -> context -> context
80601
4e8845bbcd81 clarified signature: more robust operations;
wenzelm
parents: 80599
diff changeset
    29
  val declare_renamed: string * string -> context -> context
20158
b71f4f4c6b1e export make_context, is_declared;
wenzelm
parents: 20121
diff changeset
    30
  val is_declared: context -> string -> bool
43329
84472e198515 tuned signature: Name.invent and Name.invent_names;
wenzelm
parents: 43326
diff changeset
    31
  val invent: context -> string -> int -> string list
81513
d11ed1bf0ad2 tuned signature: more operations;
wenzelm
parents: 81509
diff changeset
    32
  val invent': string -> int -> context -> string list * context
81507
08574da77b4a clarified signature: shorten common cases;
wenzelm
parents: 80601
diff changeset
    33
  val invent_global: string -> int -> string list
08574da77b4a clarified signature: shorten common cases;
wenzelm
parents: 80601
diff changeset
    34
  val invent_global_types: int -> string list
43329
84472e198515 tuned signature: Name.invent and Name.invent_names;
wenzelm
parents: 43326
diff changeset
    35
  val invent_names: context -> string -> 'a list -> (string * 'a) list
81507
08574da77b4a clarified signature: shorten common cases;
wenzelm
parents: 80601
diff changeset
    36
  val invent_names_global: string -> 'a list -> (string * 'a) list
81509
08d492f6c1b5 tuned signature: more operations;
wenzelm
parents: 81507
diff changeset
    37
  val invent_types: context -> 'a list -> (string * 'a) list
81507
08574da77b4a clarified signature: shorten common cases;
wenzelm
parents: 80601
diff changeset
    38
  val invent_types_global: 'a list -> (string * 'a) list
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    39
  val invent_list: string list -> string -> int -> string list
43326
47cf4bc789aa simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents: 43324
diff changeset
    40
  val variant: string -> context -> string * context
80599
f8c070249502 clarified modules (see also e063c0403650);
wenzelm
parents: 77846
diff changeset
    41
  val variant_bound: string -> context -> string * context
81517
1b33a7a3c80c tuned signature: more operations;
wenzelm
parents: 81513
diff changeset
    42
  val variant_names: context -> (string * 'a) list -> (string * 'a) list
1b33a7a3c80c tuned signature: more operations;
wenzelm
parents: 81513
diff changeset
    43
  val variant_names_build: (context -> context) -> (string * 'a) list -> (string * 'a) list
81521
1bfad73ab115 clarified signature: more operations;
wenzelm
parents: 81517
diff changeset
    44
  val variants: context -> string list -> string list
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    45
  val variant_list: string list -> string list -> string list
56812
baef1c110f12 centralized upper/lowercase name mangling
haftmann
parents: 56811
diff changeset
    46
  val enforce_case: bool -> string -> string
56811
b66639331db5 optional case enforcement
haftmann
parents: 55949
diff changeset
    47
  val desymbolize: bool option -> string -> string
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    48
end;
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    49
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    50
structure Name: NAME =
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    51
struct
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    52
24849
193a10e6bf90 added uu, aT;
wenzelm
parents: 21565
diff changeset
    53
(** common defaults **)
193a10e6bf90 added uu, aT;
wenzelm
parents: 21565
diff changeset
    54
193a10e6bf90 added uu, aT;
wenzelm
parents: 21565
diff changeset
    55
val uu = "uu";
43683
b5d1873449fb tuned errors;
wenzelm
parents: 43329
diff changeset
    56
val uu_ = "uu_";
24849
193a10e6bf90 added uu, aT;
wenzelm
parents: 21565
diff changeset
    57
val aT = "'a";
193a10e6bf90 added uu, aT;
wenzelm
parents: 21565
diff changeset
    58
193a10e6bf90 added uu, aT;
wenzelm
parents: 21565
diff changeset
    59
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    60
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    61
(** special variable names **)
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    62
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    63
(* encoded bounds *)
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    64
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    65
(*names for numbered variables --
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    66
  preserves order wrt. int_ord vs. string_ord, avoids allocating new strings*)
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 small_int = Vector.tabulate (1000, fn i =>
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    69
  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
    70
  in ":" ^ leading ^ string_of_int i end);
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    71
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    72
fun bound n =
77846
5ba68d3bd741 more operations, following Isabelle/ML conventions;
wenzelm
parents: 74411
diff changeset
    73
  if n < 1000 then Vector.nth small_int n
5ba68d3bd741 more operations, following Isabelle/ML conventions;
wenzelm
parents: 74411
diff changeset
    74
  else ":" ^ bound (n div 1000) ^ Vector.nth small_int (n mod 1000);
20089
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 is_bound = String.isPrefix ":";
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    77
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    78
55949
4766342e8376 clarified check of internal names;
wenzelm
parents: 55948
diff changeset
    79
(* internal names -- NB: internal subsumes skolem *)
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    80
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    81
val internal = suffix "_";
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    82
val dest_internal = unsuffix "_";
55948
bb21b380f65d tuned signature;
wenzelm
parents: 43683
diff changeset
    83
val is_internal = String.isSuffix "_";
55949
4766342e8376 clarified check of internal names;
wenzelm
parents: 55948
diff changeset
    84
fun reject_internal (x, ps) =
4766342e8376 clarified check of internal names;
wenzelm
parents: 55948
diff changeset
    85
  if is_internal x then error ("Bad name: " ^ quote x ^ Position.here_list ps) else ();
20089
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
val skolem = suffix "__";
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    88
val dest_skolem = unsuffix "__";
55948
bb21b380f65d tuned signature;
wenzelm
parents: 43683
diff changeset
    89
val is_skolem = String.isSuffix "__";
55949
4766342e8376 clarified check of internal names;
wenzelm
parents: 55948
diff changeset
    90
fun reject_skolem (x, ps) =
4766342e8376 clarified check of internal names;
wenzelm
parents: 55948
diff changeset
    91
  if is_skolem x then error ("Bad name: " ^ quote x ^ Position.here_list ps) else ();
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    92
20099
bc3f9cb33645 clean: no special treatment of empty name;
wenzelm
parents: 20089
diff changeset
    93
fun clean_index (x, i) =
bc3f9cb33645 clean: no special treatment of empty name;
wenzelm
parents: 20089
diff changeset
    94
  (case try dest_internal x of
bc3f9cb33645 clean: no special treatment of empty name;
wenzelm
parents: 20089
diff changeset
    95
    NONE => (x, i)
bc3f9cb33645 clean: no special treatment of empty name;
wenzelm
parents: 20089
diff changeset
    96
  | SOME x' => clean_index (x', i + 1));
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    97
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    98
fun clean x = #1 (clean_index (x, 0));
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
    99
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   100
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   101
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   102
(** generating fresh names **)
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   103
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   104
(* context *)
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   105
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   106
datatype context =
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   107
  Context of string option Symtab.table;    (*declared names with latest renaming*)
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   108
20099
bc3f9cb33645 clean: no special treatment of empty name;
wenzelm
parents: 20089
diff changeset
   109
fun declare x (Context tab) =
bc3f9cb33645 clean: no special treatment of empty name;
wenzelm
parents: 20089
diff changeset
   110
  Context (Symtab.default (clean x, NONE) tab);
bc3f9cb33645 clean: no special treatment of empty name;
wenzelm
parents: 20089
diff changeset
   111
bc3f9cb33645 clean: no special treatment of empty name;
wenzelm
parents: 20089
diff changeset
   112
fun declare_renaming (x, x') (Context tab) =
bc3f9cb33645 clean: no special treatment of empty name;
wenzelm
parents: 20089
diff changeset
   113
  Context (Symtab.update (clean x, SOME (clean x')) tab);
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   114
80601
4e8845bbcd81 clarified signature: more robust operations;
wenzelm
parents: 80599
diff changeset
   115
fun declare_renamed (x, x') =
4e8845bbcd81 clarified signature: more robust operations;
wenzelm
parents: 80599
diff changeset
   116
  clean x <> clean x' ? declare_renaming (x, x') #> declare x';
4e8845bbcd81 clarified signature: more robust operations;
wenzelm
parents: 80599
diff changeset
   117
20158
b71f4f4c6b1e export make_context, is_declared;
wenzelm
parents: 20121
diff changeset
   118
fun is_declared (Context tab) = Symtab.defined tab;
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   119
fun declared (Context tab) = Symtab.lookup tab;
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   120
20121
848fc1a1d355 do not export make_context;
wenzelm
parents: 20104
diff changeset
   121
val context = Context Symtab.empty |> fold declare ["", "'"];
74411
20b0b27bc6c7 clarified signature;
wenzelm
parents: 69137
diff changeset
   122
20b0b27bc6c7 clarified signature;
wenzelm
parents: 69137
diff changeset
   123
fun build_context (f: context -> context) = f context;
20b0b27bc6c7 clarified signature;
wenzelm
parents: 69137
diff changeset
   124
20b0b27bc6c7 clarified signature;
wenzelm
parents: 69137
diff changeset
   125
val make_context = build_context o fold declare;
20089
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
43329
84472e198515 tuned signature: Name.invent and Name.invent_names;
wenzelm
parents: 43326
diff changeset
   128
(* invent names *)
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   129
43329
84472e198515 tuned signature: Name.invent and Name.invent_names;
wenzelm
parents: 43326
diff changeset
   130
fun invent ctxt =
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   131
  let
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   132
    fun invs _ 0 = []
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   133
      | invs x n =
43329
84472e198515 tuned signature: Name.invent and Name.invent_names;
wenzelm
parents: 43326
diff changeset
   134
          let val x' = Symbol.bump_string x
84472e198515 tuned signature: Name.invent and Name.invent_names;
wenzelm
parents: 43326
diff changeset
   135
          in if is_declared ctxt x then invs x' n else x :: invs x' (n - 1) end;
20099
bc3f9cb33645 clean: no special treatment of empty name;
wenzelm
parents: 20089
diff changeset
   136
  in invs o clean end;
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   137
81513
d11ed1bf0ad2 tuned signature: more operations;
wenzelm
parents: 81509
diff changeset
   138
fun invent' x n ctxt =
d11ed1bf0ad2 tuned signature: more operations;
wenzelm
parents: 81509
diff changeset
   139
  let val xs = invent ctxt x n
d11ed1bf0ad2 tuned signature: more operations;
wenzelm
parents: 81509
diff changeset
   140
  in (xs, fold declare xs ctxt) end;
d11ed1bf0ad2 tuned signature: more operations;
wenzelm
parents: 81509
diff changeset
   141
81507
08574da77b4a clarified signature: shorten common cases;
wenzelm
parents: 80601
diff changeset
   142
val invent_global = invent context;
08574da77b4a clarified signature: shorten common cases;
wenzelm
parents: 80601
diff changeset
   143
val invent_global_types = invent_global aT;
08574da77b4a clarified signature: shorten common cases;
wenzelm
parents: 80601
diff changeset
   144
43329
84472e198515 tuned signature: Name.invent and Name.invent_names;
wenzelm
parents: 43326
diff changeset
   145
fun invent_names ctxt x xs = invent ctxt x (length xs) ~~ xs;
81507
08574da77b4a clarified signature: shorten common cases;
wenzelm
parents: 80601
diff changeset
   146
fun invent_names_global x xs = invent_names context x xs;
81509
08d492f6c1b5 tuned signature: more operations;
wenzelm
parents: 81507
diff changeset
   147
08d492f6c1b5 tuned signature: more operations;
wenzelm
parents: 81507
diff changeset
   148
fun invent_types ctxt xs = invent_names ctxt aT xs;
08d492f6c1b5 tuned signature: more operations;
wenzelm
parents: 81507
diff changeset
   149
fun invent_types_global xs = invent_types context xs;
20198
wenzelm
parents: 20192
diff changeset
   150
43329
84472e198515 tuned signature: Name.invent and Name.invent_names;
wenzelm
parents: 43326
diff changeset
   151
val invent_list = invent o make_context;
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   152
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   153
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   154
(* variants *)
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   155
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   156
(*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
   157
  context; preserves a suffix of underscores "_"*)
43326
47cf4bc789aa simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents: 43324
diff changeset
   158
fun variant name ctxt =
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   159
  let
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   160
    fun vary x =
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   161
      (case declared ctxt x of
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   162
        NONE => x
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   163
      | SOME x' => vary (Symbol.bump_string (the_default x x')));
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   164
20121
848fc1a1d355 do not export make_context;
wenzelm
parents: 20104
diff changeset
   165
    val (x, n) = clean_index (name, 0);
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   166
    val (x', ctxt') =
20158
b71f4f4c6b1e export make_context, is_declared;
wenzelm
parents: 20121
diff changeset
   167
      if not (is_declared ctxt x) then (x, declare x ctxt)
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   168
      else
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   169
        let
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   170
          val x0 = Symbol.bump_init x;
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   171
          val x' = vary x0;
80601
4e8845bbcd81 clarified signature: more robust operations;
wenzelm
parents: 80599
diff changeset
   172
          val ctxt' = ctxt |> declare_renamed (x0, x');
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   173
        in (x', ctxt') end;
43326
47cf4bc789aa simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents: 43324
diff changeset
   174
  in (x' ^ replicate_string n "_", ctxt') end;
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   175
80599
f8c070249502 clarified modules (see also e063c0403650);
wenzelm
parents: 77846
diff changeset
   176
fun variant_bound name = variant (if is_bound name then "u" else name);
f8c070249502 clarified modules (see also e063c0403650);
wenzelm
parents: 77846
diff changeset
   177
81517
1b33a7a3c80c tuned signature: more operations;
wenzelm
parents: 81513
diff changeset
   178
fun variant_names ctxt xs = #1 (fold_map (variant o fst) xs ctxt) ~~ map snd xs;
1b33a7a3c80c tuned signature: more operations;
wenzelm
parents: 81513
diff changeset
   179
fun variant_names_build f xs = variant_names (build_context f) xs;
1b33a7a3c80c tuned signature: more operations;
wenzelm
parents: 81513
diff changeset
   180
81521
1bfad73ab115 clarified signature: more operations;
wenzelm
parents: 81517
diff changeset
   181
fun variants ctxt xs = #1 (fold_map variant xs ctxt);
1bfad73ab115 clarified signature: more operations;
wenzelm
parents: 81517
diff changeset
   182
val variant_list = variants o make_context;
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   183
31013
69a476d6fea6 Symbol.name_of and Name.desymbolize
haftmann
parents: 30584
diff changeset
   184
31035
de0a20a030fd desymbolization with case selection
haftmann
parents: 31013
diff changeset
   185
(* names conforming to typical requirements of identifiers in the world outside *)
31013
69a476d6fea6 Symbol.name_of and Name.desymbolize
haftmann
parents: 30584
diff changeset
   186
69137
90fce429e1bc Jenkins: run ocaml_setup
Lars Hupel <lars.hupel@mytum.de>
parents: 56812
diff changeset
   187
fun enforce_case' false cs =
56812
baef1c110f12 centralized upper/lowercase name mangling
haftmann
parents: 56811
diff changeset
   188
      (if forall Symbol.is_ascii_upper cs then map else nth_map 0) Symbol.to_ascii_lower cs
69137
90fce429e1bc Jenkins: run ocaml_setup
Lars Hupel <lars.hupel@mytum.de>
parents: 56812
diff changeset
   189
  | enforce_case' true cs =
56812
baef1c110f12 centralized upper/lowercase name mangling
haftmann
parents: 56811
diff changeset
   190
      nth_map 0 Symbol.to_ascii_upper cs;
baef1c110f12 centralized upper/lowercase name mangling
haftmann
parents: 56811
diff changeset
   191
baef1c110f12 centralized upper/lowercase name mangling
haftmann
parents: 56811
diff changeset
   192
fun enforce_case upper = implode o enforce_case' upper o raw_explode;
baef1c110f12 centralized upper/lowercase name mangling
haftmann
parents: 56811
diff changeset
   193
56811
b66639331db5 optional case enforcement
haftmann
parents: 55949
diff changeset
   194
fun desymbolize perhaps_upper "" =
b66639331db5 optional case enforcement
haftmann
parents: 55949
diff changeset
   195
      if the_default false perhaps_upper then "X" else "x"
b66639331db5 optional case enforcement
haftmann
parents: 55949
diff changeset
   196
  | desymbolize perhaps_upper s =
31013
69a476d6fea6 Symbol.name_of and Name.desymbolize
haftmann
parents: 30584
diff changeset
   197
      let
31035
de0a20a030fd desymbolization with case selection
haftmann
parents: 31013
diff changeset
   198
        val xs as (x :: _) = Symbol.explode s;
33547
edfc35dcfe31 desymbolize: use Symbol.decode directly;
wenzelm
parents: 33095
diff changeset
   199
        val ys =
edfc35dcfe31 desymbolize: use Symbol.decode directly;
wenzelm
parents: 33095
diff changeset
   200
          if Symbol.is_ascii_letter x orelse Symbol.is_symbolic x then xs
31013
69a476d6fea6 Symbol.name_of and Name.desymbolize
haftmann
parents: 30584
diff changeset
   201
          else "x" :: xs;
69a476d6fea6 Symbol.name_of and Name.desymbolize
haftmann
parents: 30584
diff changeset
   202
        fun is_valid x =
34307
9074aa7d06e0 single quote is not a valid letter any more
haftmann
parents: 33547
diff changeset
   203
          Symbol.is_ascii_letter x orelse Symbol.is_ascii_digit x;
31013
69a476d6fea6 Symbol.name_of and Name.desymbolize
haftmann
parents: 30584
diff changeset
   204
        fun sep [] = []
69a476d6fea6 Symbol.name_of and Name.desymbolize
haftmann
parents: 30584
diff changeset
   205
          | sep (xs as "_" :: _) = xs
69a476d6fea6 Symbol.name_of and Name.desymbolize
haftmann
parents: 30584
diff changeset
   206
          | sep xs = "_" :: xs;
31035
de0a20a030fd desymbolization with case selection
haftmann
parents: 31013
diff changeset
   207
        fun desep ("_" :: xs) = xs
de0a20a030fd desymbolization with case selection
haftmann
parents: 31013
diff changeset
   208
          | desep xs = xs;
33547
edfc35dcfe31 desymbolize: use Symbol.decode directly;
wenzelm
parents: 33095
diff changeset
   209
        fun desymb x xs =
edfc35dcfe31 desymbolize: use Symbol.decode directly;
wenzelm
parents: 33095
diff changeset
   210
          if is_valid x then x :: xs
31013
69a476d6fea6 Symbol.name_of and Name.desymbolize
haftmann
parents: 30584
diff changeset
   211
          else
33547
edfc35dcfe31 desymbolize: use Symbol.decode directly;
wenzelm
parents: 33095
diff changeset
   212
            (case Symbol.decode x of
40627
becf5d5187cc renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents: 34307
diff changeset
   213
              Symbol.Sym name => "_" :: raw_explode name @ sep xs
33547
edfc35dcfe31 desymbolize: use Symbol.decode directly;
wenzelm
parents: 33095
diff changeset
   214
            | _ => sep xs);
56812
baef1c110f12 centralized upper/lowercase name mangling
haftmann
parents: 56811
diff changeset
   215
        val upper_lower = Option.map enforce_case' perhaps_upper |> the_default I;
31035
de0a20a030fd desymbolization with case selection
haftmann
parents: 31013
diff changeset
   216
      in fold_rev desymb ys [] |> desep |> upper_lower |> implode end;
31013
69a476d6fea6 Symbol.name_of and Name.desymbolize
haftmann
parents: 30584
diff changeset
   217
20089
d757ebadb0a2 Names of basic logical entities (variables etc.).
wenzelm
parents:
diff changeset
   218
end;