author | wenzelm |
Tue, 29 Mar 2016 21:17:29 +0200 | |
changeset 62752 | d09d71223e7a |
parent 59841 | 2551ac44150e |
child 62753 | 76b814ccce61 |
permissions | -rw-r--r-- |
384 | 1 |
(* Title: Pure/Syntax/mixfix.ML |
551 | 2 |
Author: Tobias Nipkow and Markus Wenzel, TU Muenchen |
384 | 3 |
|
18719
dca3ae4f6dd6
moved pure syntax to Syntax/syntax.ML and pure_thy.ML;
wenzelm
parents:
18673
diff
changeset
|
4 |
Mixfix declarations, infixes, binders. |
384 | 5 |
*) |
6 |
||
42287
d98eb048a2e4
discontinued special treatment of structure Mixfix;
wenzelm
parents:
42284
diff
changeset
|
7 |
signature BASIC_MIXFIX = |
2199
bcb360f80dac
added Infixl/rName: specify infix name independently from syntax;
wenzelm
parents:
1952
diff
changeset
|
8 |
sig |
384 | 9 |
datatype mixfix = |
10 |
NoSyn | |
|
62752 | 11 |
Mixfix of Input.source * int list * int * Position.range | |
12 |
Delimfix of Input.source * Position.range | |
|
13 |
Infix of Input.source * int * Position.range | |
|
14 |
Infixl of Input.source * int * Position.range | |
|
15 |
Infixr of Input.source * int * Position.range | |
|
16 |
Binder of Input.source * int * int * Position.range | |
|
17 |
Structure of Position.range |
|
2199
bcb360f80dac
added Infixl/rName: specify infix name independently from syntax;
wenzelm
parents:
1952
diff
changeset
|
18 |
end; |
384 | 19 |
|
42287
d98eb048a2e4
discontinued special treatment of structure Mixfix;
wenzelm
parents:
42284
diff
changeset
|
20 |
signature MIXFIX = |
2199
bcb360f80dac
added Infixl/rName: specify infix name independently from syntax;
wenzelm
parents:
1952
diff
changeset
|
21 |
sig |
42287
d98eb048a2e4
discontinued special treatment of structure Mixfix;
wenzelm
parents:
42284
diff
changeset
|
22 |
include BASIC_MIXFIX |
62752 | 23 |
val is_empty: mixfix -> bool |
24 |
val equal: mixfix * mixfix -> bool |
|
25 |
val set_range: Position.range -> mixfix -> mixfix |
|
26 |
val range_of: mixfix -> Position.range |
|
27 |
val pos_of: mixfix -> Position.T |
|
19271 | 28 |
val pretty_mixfix: mixfix -> Pretty.T |
4823 | 29 |
val mixfix_args: mixfix -> int |
22702 | 30 |
val mixfixT: mixfix -> typ |
35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
35390
diff
changeset
|
31 |
val make_type: int -> typ |
42287
d98eb048a2e4
discontinued special treatment of structure Mixfix;
wenzelm
parents:
42284
diff
changeset
|
32 |
val binder_name: string -> string |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
33 |
val syn_ext_types: (string * typ * mixfix) list -> Syntax_Ext.syn_ext |
59841 | 34 |
val syn_ext_consts: string list -> (string * typ * mixfix) list -> Syntax_Ext.syn_ext |
2199
bcb360f80dac
added Infixl/rName: specify infix name independently from syntax;
wenzelm
parents:
1952
diff
changeset
|
35 |
end; |
bcb360f80dac
added Infixl/rName: specify infix name independently from syntax;
wenzelm
parents:
1952
diff
changeset
|
36 |
|
15751
65e4790c7914
identify binder translations only once (admits remove);
wenzelm
parents:
15570
diff
changeset
|
37 |
structure Mixfix: MIXFIX = |
384 | 38 |
struct |
39 |
||
40 |
(** mixfix declarations **) |
|
41 |
||
42 |
datatype mixfix = |
|
43 |
NoSyn | |
|
62752 | 44 |
Mixfix of Input.source * int list * int * Position.range | |
45 |
Delimfix of Input.source * Position.range | |
|
46 |
Infix of Input.source * int * Position.range | |
|
47 |
Infixl of Input.source * int * Position.range | |
|
48 |
Infixr of Input.source * int * Position.range | |
|
49 |
Binder of Input.source * int * int * Position.range | |
|
50 |
Structure of Position.range; |
|
51 |
||
52 |
fun is_empty NoSyn = true |
|
53 |
| is_empty _ = false; |
|
54 |
||
55 |
fun equal (NoSyn, NoSyn) = true |
|
56 |
| equal (Mixfix (sy, ps, p, _), Mixfix (sy', ps', p', _)) = |
|
57 |
Input.equal_content (sy, sy') andalso ps = ps' andalso p = p' |
|
58 |
| equal (Delimfix (sy, _), Delimfix (sy', _)) = Input.equal_content (sy, sy') |
|
59 |
| equal (Infix (sy, p, _), Infix (sy', p', _)) = Input.equal_content (sy, sy') andalso p = p' |
|
60 |
| equal (Infixl (sy, p, _), Infixl (sy', p', _)) = Input.equal_content (sy, sy') andalso p = p' |
|
61 |
| equal (Infixr (sy, p, _), Infixr (sy', p', _)) = Input.equal_content (sy, sy') andalso p = p' |
|
62 |
| equal (Binder (sy, p, q, _), Binder (sy', p', q', _)) = |
|
63 |
Input.equal_content (sy, sy') andalso p = p' andalso q = q' |
|
64 |
| equal (Structure _, Structure _) = true |
|
65 |
| equal _ = false; |
|
66 |
||
67 |
fun set_range range mx = |
|
68 |
(case mx of |
|
69 |
NoSyn => NoSyn |
|
70 |
| Mixfix (sy, ps, p, _) => Mixfix (sy, ps, p, range) |
|
71 |
| Delimfix (sy, _) => Delimfix (sy, range) |
|
72 |
| Infix (sy, p, _) => Infix (sy, p, range) |
|
73 |
| Infixl (sy, p, _) => Infixl (sy, p, range) |
|
74 |
| Infixr (sy, p, _) => Infixr (sy, p, range) |
|
75 |
| Binder (sy, p, q, _) => Binder (sy, p, q, range) |
|
76 |
| Structure _ => Structure range); |
|
77 |
||
78 |
fun range_of NoSyn = Position.no_range |
|
79 |
| range_of (Mixfix (_, _, _, range)) = range |
|
80 |
| range_of (Delimfix (_, range)) = range |
|
81 |
| range_of (Infix (_, _, range)) = range |
|
82 |
| range_of (Infixl (_, _, range)) = range |
|
83 |
| range_of (Infixr (_, _, range)) = range |
|
84 |
| range_of (Binder (_, _, _, range)) = range |
|
85 |
| range_of (Structure range) = range; |
|
86 |
||
87 |
val pos_of = Position.set_range o range_of; |
|
384 | 88 |
|
89 |
||
19271 | 90 |
(* pretty_mixfix *) |
91 |
||
92 |
local |
|
11920 | 93 |
|
62752 | 94 |
val quoted = Pretty.quote o Pretty.str o Input.source_content; |
55763 | 95 |
val keyword = Pretty.keyword2; |
19271 | 96 |
val parens = Pretty.enclose "(" ")"; |
97 |
val brackets = Pretty.enclose "[" "]"; |
|
98 |
val int = Pretty.str o string_of_int; |
|
99 |
||
100 |
in |
|
11920 | 101 |
|
19271 | 102 |
fun pretty_mixfix NoSyn = Pretty.str "" |
62752 | 103 |
| pretty_mixfix (Mixfix (s, ps, p, _)) = |
19271 | 104 |
parens (Pretty.breaks [quoted s, brackets (Pretty.commas (map int ps)), int p]) |
62752 | 105 |
| pretty_mixfix (Delimfix (s, _)) = parens [quoted s] |
106 |
| pretty_mixfix (Infix (s, p, _)) = parens (Pretty.breaks [keyword "infix", quoted s, int p]) |
|
107 |
| pretty_mixfix (Infixl (s, p, _)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p]) |
|
108 |
| pretty_mixfix (Infixr (s, p, _)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p]) |
|
109 |
| pretty_mixfix (Binder (s, p1, p2, _)) = |
|
19271 | 110 |
parens (Pretty.breaks [keyword "binder", quoted s, brackets [int p1], int p2]) |
62752 | 111 |
| pretty_mixfix (Structure _) = parens [keyword "structure"]; |
19271 | 112 |
|
113 |
end; |
|
11920 | 114 |
|
115 |
||
12531 | 116 |
(* syntax specifications *) |
384 | 117 |
|
4053 | 118 |
fun mixfix_args NoSyn = 0 |
62752 | 119 |
| mixfix_args (Mixfix (sy, _, _, _)) = Syntax_Ext.mixfix_args sy |
120 |
| mixfix_args (Delimfix (sy, _)) = Syntax_Ext.mixfix_args sy |
|
121 |
| mixfix_args (Infix (sy, _, _)) = 2 + Syntax_Ext.mixfix_args sy |
|
122 |
| mixfix_args (Infixl (sy, _, _)) = 2 + Syntax_Ext.mixfix_args sy |
|
123 |
| mixfix_args (Infixr (sy, _, _)) = 2 + Syntax_Ext.mixfix_args sy |
|
18673 | 124 |
| mixfix_args (Binder _) = 1 |
62752 | 125 |
| mixfix_args (Structure _) = 0; |
4053 | 126 |
|
22709 | 127 |
fun mixfixT (Binder _) = (dummyT --> dummyT) --> dummyT |
128 |
| mixfixT mx = replicate (mixfix_args mx) dummyT ---> dummyT; |
|
22702 | 129 |
|
4053 | 130 |
|
384 | 131 |
(* syn_ext_types *) |
132 |
||
42293
6cca0343ea48
renamed sprop "prop#" to "prop'" -- proper identifier;
wenzelm
parents:
42288
diff
changeset
|
133 |
val typeT = Type ("type", []); |
6cca0343ea48
renamed sprop "prop#" to "prop'" -- proper identifier;
wenzelm
parents:
42288
diff
changeset
|
134 |
fun make_type n = replicate n typeT ---> typeT; |
35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
35390
diff
changeset
|
135 |
|
14903 | 136 |
fun syn_ext_types type_decls = |
384 | 137 |
let |
62752 | 138 |
fun mk_infix sy ty t p1 p2 p3 = |
139 |
Syntax_Ext.Mfix |
|
140 |
(Symbol_Pos.explode0 "(_ " @ Input.source_explode sy @ Symbol_Pos.explode0 "/ _)", |
|
141 |
ty, t, [p1, p2], p3); |
|
384 | 142 |
|
35129 | 143 |
fun mfix_of (_, _, NoSyn) = NONE |
62752 | 144 |
| mfix_of (t, ty, Mixfix (sy, ps, p, _)) = |
145 |
SOME (Syntax_Ext.Mfix (Input.source_explode sy, ty, t, ps, p)) |
|
146 |
| mfix_of (t, ty, Delimfix (sy, _)) = |
|
147 |
SOME (Syntax_Ext.Mfix (Input.source_explode sy, ty, t, [], 1000)) |
|
148 |
| mfix_of (t, ty, Infix (sy, p, _)) = SOME (mk_infix sy ty t (p + 1) (p + 1) p) |
|
149 |
| mfix_of (t, ty, Infixl (sy, p, _)) = SOME (mk_infix sy ty t p (p + 1) p) |
|
150 |
| mfix_of (t, ty, Infixr (sy, p, _)) = SOME (mk_infix sy ty t (p + 1) p p) |
|
42130
e10e2cce85c8
removed unclear comments stemming from ed24ba6f69aa;
wenzelm
parents:
37216
diff
changeset
|
151 |
| mfix_of (t, _, _) = error ("Bad mixfix declaration for " ^ quote t); |
384 | 152 |
|
62752 | 153 |
fun check_args (_, ty, mx) (SOME (mfix as Syntax_Ext.Mfix (sy, _, _, _, _))) = |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
154 |
if length (Term.binder_types ty) = Syntax_Ext.mfix_args sy then () |
62752 | 155 |
else |
156 |
error ("Bad number of type constructor arguments in mixfix annotation" ^ |
|
157 |
Position.here (pos_of mx)) |
|
35351
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
158 |
| check_args _ NONE = (); |
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
159 |
|
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
160 |
val mfix = map mfix_of type_decls; |
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
161 |
val _ = map2 check_args type_decls mfix; |
42298
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
162 |
val consts = map (fn (t, _, _) => (t, "")) type_decls; |
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
163 |
in Syntax_Ext.syn_ext (map_filter I mfix) consts ([], [], [], []) ([], []) end; |
384 | 164 |
|
165 |
||
166 |
(* syn_ext_consts *) |
|
167 |
||
15751
65e4790c7914
identify binder translations only once (admits remove);
wenzelm
parents:
15570
diff
changeset
|
168 |
val binder_stamp = stamp (); |
21534
68f805e9db0b
Binder: syntax const is determined by binder_name, not its syntax;
wenzelm
parents:
20892
diff
changeset
|
169 |
val binder_name = suffix "_binder"; |
15751
65e4790c7914
identify binder translations only once (admits remove);
wenzelm
parents:
15570
diff
changeset
|
170 |
|
59841 | 171 |
fun syn_ext_consts logical_types const_decls = |
384 | 172 |
let |
173 |
fun mk_infix sy ty c p1 p2 p3 = |
|
62752 | 174 |
[Syntax_Ext.Mfix |
175 |
(Symbol_Pos.explode0 "op " @ Input.source_explode sy, ty, c, [], 1000), |
|
176 |
Syntax_Ext.Mfix |
|
177 |
(Symbol_Pos.explode0 "(_ " @ Input.source_explode sy @ Symbol_Pos.explode0 "/ _)", |
|
178 |
ty, c, [p1, p2], p3)]; |
|
384 | 179 |
|
180 |
fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) = |
|
181 |
[Type ("idts", []), ty2] ---> ty3 |
|
14903 | 182 |
| binder_typ c _ = error ("Bad type of binder: " ^ quote c); |
384 | 183 |
|
35129 | 184 |
fun mfix_of (_, _, NoSyn) = [] |
62752 | 185 |
| mfix_of (c, ty, Mixfix (sy, ps, p, _)) = |
186 |
[Syntax_Ext.Mfix (Input.source_explode sy, ty, c, ps, p)] |
|
187 |
| mfix_of (c, ty, Delimfix (sy, _)) = |
|
188 |
[Syntax_Ext.Mfix (Input.source_explode sy, ty, c, [], 1000)] |
|
189 |
| mfix_of (c, ty, Infix (sy, p, _)) = mk_infix sy ty c (p + 1) (p + 1) p |
|
190 |
| mfix_of (c, ty, Infixl (sy, p, _)) = mk_infix sy ty c p (p + 1) p |
|
191 |
| mfix_of (c, ty, Infixr (sy, p, _)) = mk_infix sy ty c (p + 1) p p |
|
192 |
| mfix_of (c, ty, Binder (sy, p, q, _)) = |
|
193 |
[Syntax_Ext.Mfix |
|
194 |
(Symbol_Pos.explode0 "(3" @ Input.source_explode sy @ Symbol_Pos.explode0 "_./ _)", |
|
195 |
binder_typ c ty, (binder_name c), [0, p], q)] |
|
196 |
| mfix_of (c, _, mx) = |
|
197 |
error ("Bad mixfix declaration for " ^ quote c ^ Position.here (pos_of mx)); |
|
384 | 198 |
|
21534
68f805e9db0b
Binder: syntax const is determined by binder_name, not its syntax;
wenzelm
parents:
20892
diff
changeset
|
199 |
fun binder (c, _, Binder _) = SOME (binder_name c, c) |
15531 | 200 |
| binder _ = NONE; |
384 | 201 |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19467
diff
changeset
|
202 |
val mfix = maps mfix_of const_decls; |
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19467
diff
changeset
|
203 |
val binders = map_filter binder const_decls; |
35129 | 204 |
val binder_trs = binders |
52143 | 205 |
|> map (Syntax_Ext.stamp_trfun binder_stamp o Syntax_Trans.mk_binder_tr); |
35129 | 206 |
val binder_trs' = binders |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
207 |
|> map (Syntax_Ext.stamp_trfun binder_stamp o |
52143 | 208 |
apsnd Syntax_Trans.non_typed_tr' o Syntax_Trans.mk_binder_tr' o swap); |
42298
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
209 |
|
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
210 |
val consts = binders @ map (fn (c, _, _) => (c, "")) const_decls; |
14903 | 211 |
in |
59841 | 212 |
Syntax_Ext.syn_ext' logical_types mfix consts ([], binder_trs, binder_trs', []) ([], []) |
14903 | 213 |
end; |
384 | 214 |
|
215 |
end; |
|
42287
d98eb048a2e4
discontinued special treatment of structure Mixfix;
wenzelm
parents:
42284
diff
changeset
|
216 |
|
d98eb048a2e4
discontinued special treatment of structure Mixfix;
wenzelm
parents:
42284
diff
changeset
|
217 |
structure Basic_Mixfix: BASIC_MIXFIX = Mixfix; |
d98eb048a2e4
discontinued special treatment of structure Mixfix;
wenzelm
parents:
42284
diff
changeset
|
218 |
open Basic_Mixfix; |