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