| author | bulwahn | 
| Thu, 02 Jun 2011 10:13:46 +0200 | |
| changeset 43149 | 9675d631df3d | 
| parent 42381 | 309ec68442c6 | 
| child 43546 | 6629e2dedb00 | 
| permissions | -rw-r--r-- | 
| 28941 | 1 | (* Title: Pure/General/binding.ML | 
| 2 | Author: Florian Haftmann, TU Muenchen | |
| 30214 
f84c9f10292a
moved name space externalization flags back to name_space.ML;
 wenzelm parents: 
29617diff
changeset | 3 | Author: Makarius | 
| 28941 | 4 | |
| 5 | Structured name bindings. | |
| 6 | *) | |
| 7 | ||
| 30222 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 8 | type bstring = string; (*primitive names to be bound*) | 
| 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 9 | |
| 30214 
f84c9f10292a
moved name space externalization flags back to name_space.ML;
 wenzelm parents: 
29617diff
changeset | 10 | signature BINDING = | 
| 28941 | 11 | sig | 
| 29581 | 12 | type binding | 
| 33157 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 13 | val dest: binding -> bool * (string * bool) list * bstring | 
| 30222 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 14 | val make: bstring * Position.T -> binding | 
| 30276 | 15 | val pos_of: binding -> Position.T | 
| 30222 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 16 | val name: bstring -> binding | 
| 30464 | 17 | val name_of: binding -> bstring | 
| 30222 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 18 | val map_name: (bstring -> bstring) -> binding -> binding | 
| 30338 | 19 | val prefix_name: string -> binding -> binding | 
| 20 | val suffix_name: string -> binding -> binding | |
| 21 | val eq_name: binding * binding -> bool | |
| 29617 | 22 | val empty: binding | 
| 30222 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 23 | val is_empty: binding -> bool | 
| 30410 | 24 | val qualify: bool -> string -> binding -> binding | 
| 35200 
aaddb2b526d6
more systematic treatment of qualified names derived from binding;
 wenzelm parents: 
33157diff
changeset | 25 | val qualified: bool -> string -> binding -> binding | 
| 30464 | 26 | val qualified_name: string -> binding | 
| 27 | val qualified_name_of: binding -> string | |
| 30276 | 28 | val prefix_of: binding -> (string * bool) list | 
| 30222 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 29 | val map_prefix: ((string * bool) list -> (string * bool) list) -> binding -> binding | 
| 30335 
b3ef64cadcad
Binding.str_of: removed verbose feature, include qualifier in output;
 wenzelm parents: 
30276diff
changeset | 30 | val prefix: bool -> string -> binding -> binding | 
| 33157 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 31 | val conceal: binding -> binding | 
| 30410 | 32 | val str_of: binding -> string | 
| 42381 
309ec68442c6
added Binding.print convenience, which includes quote already;
 wenzelm parents: 
41254diff
changeset | 33 | val print: binding -> string | 
| 41254 
78c3e472bb35
extra checking of name bindings for classes, types, consts;
 wenzelm parents: 
39442diff
changeset | 34 | val bad: binding -> string | 
| 
78c3e472bb35
extra checking of name bindings for classes, types, consts;
 wenzelm parents: 
39442diff
changeset | 35 | val check: binding -> unit | 
| 29617 | 36 | end; | 
| 28941 | 37 | |
| 32590 
95f4f08f950f
replaced opaque signature matching by plain old abstype (again, cf. ac4498f95d1c) -- this recovers pretty printing in SML/NJ and Poly/ML 5.3;
 wenzelm parents: 
30795diff
changeset | 38 | structure Binding: BINDING = | 
| 28941 | 39 | struct | 
| 40 | ||
| 30222 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 41 | (** representation **) | 
| 29338 | 42 | |
| 30222 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 43 | (* datatype *) | 
| 29338 | 44 | |
| 32590 
95f4f08f950f
replaced opaque signature matching by plain old abstype (again, cf. ac4498f95d1c) -- this recovers pretty printing in SML/NJ and Poly/ML 5.3;
 wenzelm parents: 
30795diff
changeset | 45 | abstype binding = Binding of | 
| 33157 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 46 |  {conceal: bool,                    (*internal -- for foundational purposes only*)
 | 
| 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 47 | prefix: (string * bool) list, (*system prefix*) | 
| 30276 | 48 | qualifier: (string * bool) list, (*user qualifier*) | 
| 49 | name: bstring, (*base name*) | |
| 32590 
95f4f08f950f
replaced opaque signature matching by plain old abstype (again, cf. ac4498f95d1c) -- this recovers pretty printing in SML/NJ and Poly/ML 5.3;
 wenzelm parents: 
30795diff
changeset | 50 | pos: Position.T} (*source position*) | 
| 
95f4f08f950f
replaced opaque signature matching by plain old abstype (again, cf. ac4498f95d1c) -- this recovers pretty printing in SML/NJ and Poly/ML 5.3;
 wenzelm parents: 
30795diff
changeset | 51 | with | 
| 28941 | 52 | |
| 33157 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 53 | fun make_binding (conceal, prefix, qualifier, name, pos) = | 
| 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 54 |   Binding {conceal = conceal, prefix = prefix, qualifier = qualifier, name = name, pos = pos};
 | 
| 30222 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 55 | |
| 33157 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 56 | fun map_binding f (Binding {conceal, prefix, qualifier, name, pos}) =
 | 
| 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 57 | make_binding (f (conceal, prefix, qualifier, name, pos)); | 
| 30222 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 58 | |
| 33157 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 59 | fun dest (Binding {conceal, prefix, qualifier, name, ...}) =
 | 
| 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 60 | (conceal, prefix @ qualifier, name); | 
| 30217 
894eb2034f02
renamed Binding.display to Binding.str_of, which is slightly more canonical;
 wenzelm parents: 
30214diff
changeset | 61 | |
| 30222 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 62 | |
| 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 63 | |
| 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 64 | (** basic operations **) | 
| 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 65 | |
| 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 66 | (* name and position *) | 
| 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 67 | |
| 33157 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 68 | fun make (name, pos) = make_binding (false, [], [], name, pos); | 
| 30222 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 69 | fun name name = make (name, Position.none); | 
| 28965 | 70 | |
| 30222 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 71 | fun pos_of (Binding {pos, ...}) = pos;
 | 
| 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 72 | fun name_of (Binding {name, ...}) = name;
 | 
| 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 73 | |
| 30338 | 74 | fun eq_name (b, b') = name_of b = name_of b'; | 
| 75 | ||
| 33157 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 76 | fun map_name f = | 
| 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 77 | map_binding (fn (conceal, prefix, qualifier, name, pos) => | 
| 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 78 | (conceal, prefix, qualifier, f name, pos)); | 
| 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 79 | |
| 30338 | 80 | val prefix_name = map_name o prefix; | 
| 81 | val suffix_name = map_name o suffix; | |
| 28941 | 82 | |
| 30222 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 83 | val empty = name ""; | 
| 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 84 | fun is_empty b = name_of b = ""; | 
| 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 85 | |
| 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 86 | |
| 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 87 | (* user qualifier *) | 
| 28941 | 88 | |
| 30410 | 89 | fun qualify _ "" = I | 
| 33157 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 90 | | qualify mandatory qual = | 
| 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 91 | map_binding (fn (conceal, prefix, qualifier, name, pos) => | 
| 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 92 | (conceal, prefix, (qual, mandatory) :: qualifier, name, pos)); | 
| 30410 | 93 | |
| 35200 
aaddb2b526d6
more systematic treatment of qualified names derived from binding;
 wenzelm parents: 
33157diff
changeset | 94 | fun qualified mandatory name' = map_binding (fn (conceal, prefix, qualifier, name, pos) => | 
| 
aaddb2b526d6
more systematic treatment of qualified names derived from binding;
 wenzelm parents: 
33157diff
changeset | 95 | let val qualifier' = if name = "" then qualifier else qualifier @ [(name, mandatory)] | 
| 
aaddb2b526d6
more systematic treatment of qualified names derived from binding;
 wenzelm parents: 
33157diff
changeset | 96 | in (conceal, prefix, qualifier', name', pos) end); | 
| 
aaddb2b526d6
more systematic treatment of qualified names derived from binding;
 wenzelm parents: 
33157diff
changeset | 97 | |
| 30361 
8ea7a197e2e6
added qualified_name -- emulates old-style qualified bstring;
 wenzelm parents: 
30338diff
changeset | 98 | fun qualified_name "" = empty | 
| 
8ea7a197e2e6
added qualified_name -- emulates old-style qualified bstring;
 wenzelm parents: 
30338diff
changeset | 99 | | qualified_name s = | 
| 
8ea7a197e2e6
added qualified_name -- emulates old-style qualified bstring;
 wenzelm parents: 
30338diff
changeset | 100 | let val (qualifier, name) = split_last (Long_Name.explode s) | 
| 33157 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 101 | in make_binding (false, [], map (rpair false) qualifier, name, Position.none) end; | 
| 30361 
8ea7a197e2e6
added qualified_name -- emulates old-style qualified bstring;
 wenzelm parents: 
30338diff
changeset | 102 | |
| 30795 | 103 | fun qualified_name_of (b as Binding {qualifier, name, ...}) =
 | 
| 104 | if is_empty b then "" | |
| 105 | else Long_Name.implode (map #1 qualifier @ [name]); | |
| 30222 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 106 | |
| 28965 | 107 | |
| 30222 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 108 | (* system prefix *) | 
| 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 109 | |
| 30276 | 110 | fun prefix_of (Binding {prefix, ...}) = prefix;
 | 
| 111 | ||
| 33157 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 112 | fun map_prefix f = | 
| 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 113 | map_binding (fn (conceal, prefix, qualifier, name, pos) => | 
| 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 114 | (conceal, f prefix, qualifier, name, pos)); | 
| 30222 
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
 wenzelm parents: 
30217diff
changeset | 115 | |
| 30335 
b3ef64cadcad
Binding.str_of: removed verbose feature, include qualifier in output;
 wenzelm parents: 
30276diff
changeset | 116 | fun prefix _ "" = I | 
| 30410 | 117 | | prefix mandatory prfx = map_prefix (cons (prfx, mandatory)); | 
| 118 | ||
| 119 | ||
| 33157 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 120 | (* conceal *) | 
| 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 121 | |
| 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 122 | val conceal = | 
| 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 123 | map_binding (fn (_, prefix, qualifier, name, pos) => | 
| 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 124 | (true, prefix, qualifier, name, pos)); | 
| 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 125 | |
| 
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
 wenzelm parents: 
32590diff
changeset | 126 | |
| 42381 
309ec68442c6
added Binding.print convenience, which includes quote already;
 wenzelm parents: 
41254diff
changeset | 127 | (* print *) | 
| 30410 | 128 | |
| 129 | fun str_of binding = | |
| 39442 | 130 | qualified_name_of binding | 
| 131 | |> Markup.markup (Position.markup (pos_of binding) (Markup.binding (name_of binding))); | |
| 28941 | 132 | |
| 42381 
309ec68442c6
added Binding.print convenience, which includes quote already;
 wenzelm parents: 
41254diff
changeset | 133 | val print = quote o str_of; | 
| 
309ec68442c6
added Binding.print convenience, which includes quote already;
 wenzelm parents: 
41254diff
changeset | 134 | |
| 41254 
78c3e472bb35
extra checking of name bindings for classes, types, consts;
 wenzelm parents: 
39442diff
changeset | 135 | |
| 
78c3e472bb35
extra checking of name bindings for classes, types, consts;
 wenzelm parents: 
39442diff
changeset | 136 | (* check *) | 
| 
78c3e472bb35
extra checking of name bindings for classes, types, consts;
 wenzelm parents: 
39442diff
changeset | 137 | |
| 42381 
309ec68442c6
added Binding.print convenience, which includes quote already;
 wenzelm parents: 
41254diff
changeset | 138 | fun bad binding = "Bad name binding: " ^ print binding ^ Position.str_of (pos_of binding); | 
| 41254 
78c3e472bb35
extra checking of name bindings for classes, types, consts;
 wenzelm parents: 
39442diff
changeset | 139 | |
| 
78c3e472bb35
extra checking of name bindings for classes, types, consts;
 wenzelm parents: 
39442diff
changeset | 140 | fun check binding = | 
| 
78c3e472bb35
extra checking of name bindings for classes, types, consts;
 wenzelm parents: 
39442diff
changeset | 141 | if Symbol.is_ident (Symbol.explode (name_of binding)) then () | 
| 
78c3e472bb35
extra checking of name bindings for classes, types, consts;
 wenzelm parents: 
39442diff
changeset | 142 | else legacy_feature (bad binding); | 
| 
78c3e472bb35
extra checking of name bindings for classes, types, consts;
 wenzelm parents: 
39442diff
changeset | 143 | |
| 28941 | 144 | end; | 
| 32590 
95f4f08f950f
replaced opaque signature matching by plain old abstype (again, cf. ac4498f95d1c) -- this recovers pretty printing in SML/NJ and Poly/ML 5.3;
 wenzelm parents: 
30795diff
changeset | 145 | end; | 
| 28941 | 146 | |
| 30214 
f84c9f10292a
moved name space externalization flags back to name_space.ML;
 wenzelm parents: 
29617diff
changeset | 147 | type binding = Binding.binding; | 
| 
f84c9f10292a
moved name space externalization flags back to name_space.ML;
 wenzelm parents: 
29617diff
changeset | 148 |