author | wenzelm |
Thu, 18 Dec 2014 21:10:39 +0100 | |
changeset 59149 | 0070053570c4 |
parent 58032 | e92cdae8b3b5 |
child 59858 | 890b68e1e8b6 |
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:
29617
diff
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:
30217
diff
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:
30217
diff
changeset
|
9 |
|
30214
f84c9f10292a
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
29617
diff
changeset
|
10 |
signature BINDING = |
28941 | 11 |
sig |
53380
08f3491c50bf
cases: formal binding of 'assumes', with position provided via invoke_case;
wenzelm
parents:
50239
diff
changeset
|
12 |
eqtype binding |
33157
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
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:
30217
diff
changeset
|
14 |
val make: bstring * Position.T -> binding |
30276 | 15 |
val pos_of: binding -> Position.T |
53380
08f3491c50bf
cases: formal binding of 'assumes', with position provided via invoke_case;
wenzelm
parents:
50239
diff
changeset
|
16 |
val set_pos: Position.T -> binding -> binding |
30222
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents:
30217
diff
changeset
|
17 |
val name: bstring -> binding |
30464 | 18 |
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:
30217
diff
changeset
|
19 |
val map_name: (bstring -> bstring) -> binding -> binding |
30338 | 20 |
val prefix_name: string -> binding -> binding |
21 |
val suffix_name: string -> binding -> binding |
|
22 |
val eq_name: binding * binding -> bool |
|
29617 | 23 |
val empty: binding |
30222
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents:
30217
diff
changeset
|
24 |
val is_empty: binding -> bool |
30410 | 25 |
val qualify: bool -> string -> binding -> binding |
35200
aaddb2b526d6
more systematic treatment of qualified names derived from binding;
wenzelm
parents:
33157
diff
changeset
|
26 |
val qualified: bool -> string -> binding -> binding |
30464 | 27 |
val qualified_name: string -> binding |
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:
30217
diff
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:
30276
diff
changeset
|
30 |
val prefix: bool -> string -> binding -> binding |
33157
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
changeset
|
31 |
val conceal: binding -> binding |
43547
f3a8476285c6
clarified Binding.pretty/print: no quotes, only markup -- Binding.str_of is rendered obsolete;
wenzelm
parents:
43546
diff
changeset
|
32 |
val pretty: binding -> Pretty.T |
42381
309ec68442c6
added Binding.print convenience, which includes quote already;
wenzelm
parents:
41254
diff
changeset
|
33 |
val print: binding -> string |
58032
e92cdae8b3b5
clarified ML toplevel pp: avoid ML output to be attached to inlined binding positions;
wenzelm
parents:
53380
diff
changeset
|
34 |
val pp: binding -> Pretty.T |
41254
78c3e472bb35
extra checking of name bindings for classes, types, consts;
wenzelm
parents:
39442
diff
changeset
|
35 |
val bad: binding -> string |
78c3e472bb35
extra checking of name bindings for classes, types, consts;
wenzelm
parents:
39442
diff
changeset
|
36 |
val check: binding -> unit |
29617 | 37 |
end; |
28941 | 38 |
|
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:
30795
diff
changeset
|
39 |
structure Binding: BINDING = |
28941 | 40 |
struct |
41 |
||
30222
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents:
30217
diff
changeset
|
42 |
(** representation **) |
29338 | 43 |
|
30222
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents:
30217
diff
changeset
|
44 |
(* datatype *) |
29338 | 45 |
|
53380
08f3491c50bf
cases: formal binding of 'assumes', with position provided via invoke_case;
wenzelm
parents:
50239
diff
changeset
|
46 |
datatype binding = Binding of |
33157
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
changeset
|
47 |
{conceal: bool, (*internal -- for foundational purposes only*) |
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
changeset
|
48 |
prefix: (string * bool) list, (*system prefix*) |
30276 | 49 |
qualifier: (string * bool) list, (*user qualifier*) |
50 |
name: bstring, (*base name*) |
|
53380
08f3491c50bf
cases: formal binding of 'assumes', with position provided via invoke_case;
wenzelm
parents:
50239
diff
changeset
|
51 |
pos: Position.T}; (*source position*) |
28941 | 52 |
|
33157
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
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:
32590
diff
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:
30217
diff
changeset
|
55 |
|
33157
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
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:
32590
diff
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:
30217
diff
changeset
|
58 |
|
33157
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
changeset
|
59 |
fun dest (Binding {conceal, prefix, qualifier, name, ...}) = |
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
changeset
|
60 |
(conceal, prefix @ qualifier, name); |
30217
894eb2034f02
renamed Binding.display to Binding.str_of, which is slightly more canonical;
wenzelm
parents:
30214
diff
changeset
|
61 |
|
30222
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents:
30217
diff
changeset
|
62 |
|
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents:
30217
diff
changeset
|
63 |
|
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents:
30217
diff
changeset
|
64 |
(** basic operations **) |
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents:
30217
diff
changeset
|
65 |
|
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents:
30217
diff
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:
30217
diff
changeset
|
67 |
|
33157
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
changeset
|
68 |
fun make (name, pos) = make_binding (false, [], [], name, pos); |
28965 | 69 |
|
30222
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents:
30217
diff
changeset
|
70 |
fun pos_of (Binding {pos, ...}) = pos; |
53380
08f3491c50bf
cases: formal binding of 'assumes', with position provided via invoke_case;
wenzelm
parents:
50239
diff
changeset
|
71 |
fun set_pos pos = |
08f3491c50bf
cases: formal binding of 'assumes', with position provided via invoke_case;
wenzelm
parents:
50239
diff
changeset
|
72 |
map_binding (fn (conceal, prefix, qualifier, name, _) => (conceal, prefix, qualifier, name, pos)); |
08f3491c50bf
cases: formal binding of 'assumes', with position provided via invoke_case;
wenzelm
parents:
50239
diff
changeset
|
73 |
|
08f3491c50bf
cases: formal binding of 'assumes', with position provided via invoke_case;
wenzelm
parents:
50239
diff
changeset
|
74 |
fun name name = make (name, Position.none); |
30222
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents:
30217
diff
changeset
|
75 |
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:
30217
diff
changeset
|
76 |
|
30338 | 77 |
fun eq_name (b, b') = name_of b = name_of b'; |
78 |
||
33157
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
changeset
|
79 |
fun map_name f = |
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
changeset
|
80 |
map_binding (fn (conceal, prefix, qualifier, name, pos) => |
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
changeset
|
81 |
(conceal, prefix, qualifier, f name, pos)); |
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
changeset
|
82 |
|
30338 | 83 |
val prefix_name = map_name o prefix; |
84 |
val suffix_name = map_name o suffix; |
|
28941 | 85 |
|
30222
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents:
30217
diff
changeset
|
86 |
val empty = name ""; |
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents:
30217
diff
changeset
|
87 |
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:
30217
diff
changeset
|
88 |
|
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents:
30217
diff
changeset
|
89 |
|
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents:
30217
diff
changeset
|
90 |
(* user qualifier *) |
28941 | 91 |
|
30410 | 92 |
fun qualify _ "" = I |
33157
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
changeset
|
93 |
| qualify mandatory qual = |
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
changeset
|
94 |
map_binding (fn (conceal, prefix, qualifier, name, pos) => |
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
changeset
|
95 |
(conceal, prefix, (qual, mandatory) :: qualifier, name, pos)); |
30410 | 96 |
|
35200
aaddb2b526d6
more systematic treatment of qualified names derived from binding;
wenzelm
parents:
33157
diff
changeset
|
97 |
fun qualified mandatory name' = map_binding (fn (conceal, prefix, qualifier, name, pos) => |
aaddb2b526d6
more systematic treatment of qualified names derived from binding;
wenzelm
parents:
33157
diff
changeset
|
98 |
let val qualifier' = if name = "" then qualifier else qualifier @ [(name, mandatory)] |
aaddb2b526d6
more systematic treatment of qualified names derived from binding;
wenzelm
parents:
33157
diff
changeset
|
99 |
in (conceal, prefix, qualifier', name', pos) end); |
aaddb2b526d6
more systematic treatment of qualified names derived from binding;
wenzelm
parents:
33157
diff
changeset
|
100 |
|
30361
8ea7a197e2e6
added qualified_name -- emulates old-style qualified bstring;
wenzelm
parents:
30338
diff
changeset
|
101 |
fun qualified_name "" = empty |
8ea7a197e2e6
added qualified_name -- emulates old-style qualified bstring;
wenzelm
parents:
30338
diff
changeset
|
102 |
| qualified_name s = |
8ea7a197e2e6
added qualified_name -- emulates old-style qualified bstring;
wenzelm
parents:
30338
diff
changeset
|
103 |
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:
32590
diff
changeset
|
104 |
in make_binding (false, [], map (rpair false) qualifier, name, Position.none) end; |
30361
8ea7a197e2e6
added qualified_name -- emulates old-style qualified bstring;
wenzelm
parents:
30338
diff
changeset
|
105 |
|
28965 | 106 |
|
30222
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents:
30217
diff
changeset
|
107 |
(* system prefix *) |
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents:
30217
diff
changeset
|
108 |
|
30276 | 109 |
fun prefix_of (Binding {prefix, ...}) = prefix; |
110 |
||
33157
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
changeset
|
111 |
fun map_prefix f = |
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
changeset
|
112 |
map_binding (fn (conceal, prefix, qualifier, name, pos) => |
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
changeset
|
113 |
(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:
30217
diff
changeset
|
114 |
|
30335
b3ef64cadcad
Binding.str_of: removed verbose feature, include qualifier in output;
wenzelm
parents:
30276
diff
changeset
|
115 |
fun prefix _ "" = I |
30410 | 116 |
| prefix mandatory prfx = map_prefix (cons (prfx, mandatory)); |
117 |
||
118 |
||
33157
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
changeset
|
119 |
(* conceal *) |
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
changeset
|
120 |
|
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
changeset
|
121 |
val conceal = |
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
changeset
|
122 |
map_binding (fn (_, prefix, qualifier, name, pos) => |
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
changeset
|
123 |
(true, prefix, qualifier, name, pos)); |
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
changeset
|
124 |
|
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
32590
diff
changeset
|
125 |
|
42381
309ec68442c6
added Binding.print convenience, which includes quote already;
wenzelm
parents:
41254
diff
changeset
|
126 |
(* print *) |
30410 | 127 |
|
43547
f3a8476285c6
clarified Binding.pretty/print: no quotes, only markup -- Binding.str_of is rendered obsolete;
wenzelm
parents:
43546
diff
changeset
|
128 |
fun pretty (Binding {prefix, qualifier, name, pos, ...}) = |
46897
ec793befc232
proper printing of empty binding (again, cf. 93f6f24010c2);
wenzelm
parents:
45666
diff
changeset
|
129 |
if name = "" then Pretty.str "\"\"" |
ec793befc232
proper printing of empty binding (again, cf. 93f6f24010c2);
wenzelm
parents:
45666
diff
changeset
|
130 |
else |
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
48992
diff
changeset
|
131 |
Pretty.markup (Position.markup pos Markup.binding) |
46897
ec793befc232
proper printing of empty binding (again, cf. 93f6f24010c2);
wenzelm
parents:
45666
diff
changeset
|
132 |
[Pretty.str (Long_Name.implode (map #1 (prefix @ qualifier) @ [name]))] |
ec793befc232
proper printing of empty binding (again, cf. 93f6f24010c2);
wenzelm
parents:
45666
diff
changeset
|
133 |
|> Pretty.quote; |
28941 | 134 |
|
43547
f3a8476285c6
clarified Binding.pretty/print: no quotes, only markup -- Binding.str_of is rendered obsolete;
wenzelm
parents:
43546
diff
changeset
|
135 |
val print = Pretty.str_of o pretty; |
42381
309ec68442c6
added Binding.print convenience, which includes quote already;
wenzelm
parents:
41254
diff
changeset
|
136 |
|
58032
e92cdae8b3b5
clarified ML toplevel pp: avoid ML output to be attached to inlined binding positions;
wenzelm
parents:
53380
diff
changeset
|
137 |
val pp = pretty o set_pos Position.none; |
e92cdae8b3b5
clarified ML toplevel pp: avoid ML output to be attached to inlined binding positions;
wenzelm
parents:
53380
diff
changeset
|
138 |
|
41254
78c3e472bb35
extra checking of name bindings for classes, types, consts;
wenzelm
parents:
39442
diff
changeset
|
139 |
|
78c3e472bb35
extra checking of name bindings for classes, types, consts;
wenzelm
parents:
39442
diff
changeset
|
140 |
(* check *) |
78c3e472bb35
extra checking of name bindings for classes, types, consts;
wenzelm
parents:
39442
diff
changeset
|
141 |
|
48992 | 142 |
fun bad binding = "Bad name binding: " ^ print binding ^ Position.here (pos_of binding); |
41254
78c3e472bb35
extra checking of name bindings for classes, types, consts;
wenzelm
parents:
39442
diff
changeset
|
143 |
|
78c3e472bb35
extra checking of name bindings for classes, types, consts;
wenzelm
parents:
39442
diff
changeset
|
144 |
fun check binding = |
50239 | 145 |
if Symbol_Pos.is_identifier (name_of binding) then () |
41254
78c3e472bb35
extra checking of name bindings for classes, types, consts;
wenzelm
parents:
39442
diff
changeset
|
146 |
else legacy_feature (bad binding); |
78c3e472bb35
extra checking of name bindings for classes, types, consts;
wenzelm
parents:
39442
diff
changeset
|
147 |
|
28941 | 148 |
end; |
149 |
||
30214
f84c9f10292a
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
29617
diff
changeset
|
150 |
type binding = Binding.binding; |
f84c9f10292a
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
29617
diff
changeset
|
151 |