39348
|
1 |
(* ========================================================================= *)
|
|
2 |
(* NAMES *)
|
39349
|
3 |
(* Copyright (c) 2004 Joe Hurd, distributed under the BSD License *)
|
39348
|
4 |
(* ========================================================================= *)
|
|
5 |
|
|
6 |
structure Name :> Name =
|
|
7 |
struct
|
|
8 |
|
|
9 |
open Useful;
|
|
10 |
|
|
11 |
(* ------------------------------------------------------------------------- *)
|
|
12 |
(* A type of names. *)
|
|
13 |
(* ------------------------------------------------------------------------- *)
|
|
14 |
|
|
15 |
type name = string;
|
|
16 |
|
|
17 |
(* ------------------------------------------------------------------------- *)
|
|
18 |
(* A total ordering. *)
|
|
19 |
(* ------------------------------------------------------------------------- *)
|
|
20 |
|
|
21 |
val compare = String.compare;
|
|
22 |
|
|
23 |
fun equal n1 n2 = n1 = n2;
|
|
24 |
|
|
25 |
(* ------------------------------------------------------------------------- *)
|
|
26 |
(* Fresh variables. *)
|
|
27 |
(* ------------------------------------------------------------------------- *)
|
|
28 |
|
|
29 |
local
|
|
30 |
val prefix = "_";
|
|
31 |
|
|
32 |
fun numName i = mkPrefix prefix (Int.toString i);
|
|
33 |
in
|
|
34 |
fun newName () = numName (newInt ());
|
|
35 |
|
|
36 |
fun newNames n = map numName (newInts n);
|
|
37 |
end;
|
|
38 |
|
|
39 |
fun variantPrime acceptable =
|
|
40 |
let
|
|
41 |
fun variant n = if acceptable n then n else variant (n ^ "'")
|
|
42 |
in
|
|
43 |
variant
|
|
44 |
end;
|
|
45 |
|
|
46 |
local
|
|
47 |
fun isDigitOrPrime #"'" = true
|
|
48 |
| isDigitOrPrime c = Char.isDigit c;
|
|
49 |
in
|
|
50 |
fun variantNum acceptable n =
|
|
51 |
if acceptable n then n
|
|
52 |
else
|
|
53 |
let
|
|
54 |
val n = stripSuffix isDigitOrPrime n
|
|
55 |
|
|
56 |
fun variant i =
|
|
57 |
let
|
|
58 |
val n_i = n ^ Int.toString i
|
|
59 |
in
|
|
60 |
if acceptable n_i then n_i else variant (i + 1)
|
|
61 |
end
|
|
62 |
in
|
|
63 |
variant 0
|
|
64 |
end;
|
|
65 |
end;
|
|
66 |
|
|
67 |
(* ------------------------------------------------------------------------- *)
|
|
68 |
(* Parsing and pretty printing. *)
|
|
69 |
(* ------------------------------------------------------------------------- *)
|
|
70 |
|
|
71 |
val pp = Print.ppString;
|
|
72 |
|
|
73 |
fun toString s : string = s;
|
|
74 |
|
|
75 |
fun fromString s : name = s;
|
|
76 |
|
|
77 |
end
|
|
78 |
|
|
79 |
structure NameOrdered =
|
|
80 |
struct type t = Name.name val compare = Name.compare end
|
|
81 |
|
|
82 |
structure NameMap = KeyMap (NameOrdered);
|
|
83 |
|
|
84 |
structure NameSet =
|
|
85 |
struct
|
|
86 |
|
|
87 |
local
|
|
88 |
structure S = ElementSet (NameMap);
|
|
89 |
in
|
|
90 |
open S;
|
|
91 |
end;
|
|
92 |
|
|
93 |
val pp =
|
|
94 |
Print.ppMap
|
|
95 |
toList
|
|
96 |
(Print.ppBracket "{" "}" (Print.ppOpList "," Name.pp));
|
|
97 |
|
|
98 |
end
|