| author | haftmann | 
| Wed, 07 Jan 2009 08:04:12 +0100 | |
| changeset 29379 | f65670092259 | 
| parent 29338 | 52a384648d13 | 
| child 29581 | b3b33e0298eb | 
| permissions | -rw-r--r-- | 
| 28941 | 1 | (* Title: Pure/General/binding.ML | 
| 2 | Author: Florian Haftmann, TU Muenchen | |
| 3 | ||
| 4 | Structured name bindings. | |
| 5 | *) | |
| 6 | ||
| 7 | signature BASIC_BINDING = | |
| 8 | sig | |
| 9 | val long_names: bool ref | |
| 10 | val short_names: bool ref | |
| 11 | val unique_names: bool ref | |
| 12 | end; | |
| 13 | ||
| 14 | signature BINDING = | |
| 15 | sig | |
| 16 | include BASIC_BINDING | |
| 17 | type T | |
| 28965 | 18 | val name_pos: string * Position.T -> T | 
| 19 | val name: string -> T | |
| 20 | val empty: T | |
| 21 | val map_base: (string -> string) -> T -> T | |
| 22 | val qualify: string -> T -> T | |
| 28941 | 23 | val add_prefix: bool -> string -> T -> T | 
| 24 | val map_prefix: ((string * bool) list -> T -> T) -> T -> T | |
| 28965 | 25 | val is_empty: T -> bool | 
| 29006 | 26 | val base_name: T -> string | 
| 28965 | 27 | val pos_of: T -> Position.T | 
| 28 | val dest: T -> (string * bool) list * string | |
| 29338 | 29 | val separator: string | 
| 30 | val is_qualified: string -> bool | |
| 28941 | 31 | val display: T -> string | 
| 32 | end | |
| 33 | ||
| 34 | structure Binding : BINDING = | |
| 35 | struct | |
| 36 | ||
| 37 | (** global flags **) | |
| 38 | ||
| 39 | val long_names = ref false; | |
| 40 | val short_names = ref false; | |
| 41 | val unique_names = ref true; | |
| 42 | ||
| 43 | ||
| 29338 | 44 | (** qualification **) | 
| 45 | ||
| 46 | val separator = "."; | |
| 47 | val is_qualified = exists_string (fn s => s = separator); | |
| 48 | ||
| 49 | fun reject_qualified kind s = | |
| 50 | if is_qualified s then | |
| 51 |     error ("Attempt to declare qualified " ^ kind ^ " " ^ quote s)
 | |
| 52 | else s; | |
| 53 | ||
| 54 | ||
| 28941 | 55 | (** binding representation **) | 
| 56 | ||
| 57 | datatype T = Binding of ((string * bool) list * string) * Position.T; | |
| 58 | (* (prefix components (with mandatory flag), base name, position) *) | |
| 59 | ||
| 28965 | 60 | fun name_pos (name, pos) = Binding (([], name), pos); | 
| 61 | fun name name = name_pos (name, Position.none); | |
| 62 | val empty = name ""; | |
| 28941 | 63 | |
| 64 | fun map_binding f (Binding (prefix_name, pos)) = Binding (f prefix_name, pos); | |
| 65 | ||
| 28965 | 66 | val map_base = map_binding o apsnd; | 
| 67 | ||
| 68 | fun qualify_base path name = | |
| 69 | if path = "" orelse name = "" then name | |
| 29338 | 70 | else path ^ separator ^ name; | 
| 28965 | 71 | |
| 72 | val qualify = map_base o qualify_base; | |
| 29006 | 73 | (*FIXME should all operations on bare names move here from name_space.ML ?*) | 
| 28941 | 74 | |
| 29208 
b0c81b9a0133
Use prefix component of bindings for locale prefixes.
 ballarin parents: 
29006diff
changeset | 75 | fun add_prefix sticky "" b = b | 
| 29338 | 76 | | add_prefix sticky prfx b = (map_binding o apfst) | 
| 77 | (cons ((*reject_qualified "prefix"*) prfx, sticky)) b; | |
| 28941 | 78 | |
| 79 | fun map_prefix f (Binding ((prefix, name), pos)) = | |
| 28965 | 80 | f prefix (name_pos (name, pos)); | 
| 81 | ||
| 82 | fun is_empty (Binding ((_, name), _)) = name = ""; | |
| 29006 | 83 | fun base_name (Binding ((_, name), _)) = name; | 
| 28965 | 84 | fun pos_of (Binding (_, pos)) = pos; | 
| 85 | fun dest (Binding (prefix_name, _)) = prefix_name; | |
| 28941 | 86 | |
| 87 | fun display (Binding ((prefix, name), _)) = | |
| 88 | let | |
| 89 | fun mk_prefix (prfx, true) = prfx | |
| 90 |       | mk_prefix (prfx, false) = enclose "(" ")" prfx
 | |
| 91 | in if not (! long_names) orelse null prefix orelse name = "" then name | |
| 92 | else space_implode "." (map mk_prefix prefix) ^ ":" ^ name | |
| 93 | end; | |
| 94 | ||
| 95 | end; | |
| 96 | ||
| 97 | structure Basic_Binding : BASIC_BINDING = Binding; | |
| 98 | open Basic_Binding; |