author | wenzelm |
Fri, 26 Feb 2010 23:05:47 +0100 | |
changeset 35390 | efad0e364738 |
parent 35351 | 7425aece4ee3 |
child 35412 | b8dead547d9e |
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 |
||
7 |
signature MIXFIX0 = |
|
2199
bcb360f80dac
added Infixl/rName: specify infix name independently from syntax;
wenzelm
parents:
1952
diff
changeset
|
8 |
sig |
384 | 9 |
datatype mixfix = |
10 |
NoSyn | |
|
11 |
Mixfix of string * int list * int | |
|
12 |
Delimfix of string | |
|
35130 | 13 |
Infix of string * int | |
14 |
Infixl of string * int | |
|
15 |
Infixr of string * int | |
|
18673 | 16 |
Binder of string * int * int | |
17 |
Structure |
|
21534
68f805e9db0b
Binder: syntax const is determined by binder_name, not its syntax;
wenzelm
parents:
20892
diff
changeset
|
18 |
val binder_name: string -> string |
2199
bcb360f80dac
added Infixl/rName: specify infix name independently from syntax;
wenzelm
parents:
1952
diff
changeset
|
19 |
end; |
384 | 20 |
|
21 |
signature MIXFIX1 = |
|
2199
bcb360f80dac
added Infixl/rName: specify infix name independently from syntax;
wenzelm
parents:
1952
diff
changeset
|
22 |
sig |
384 | 23 |
include MIXFIX0 |
4823 | 24 |
val no_syn: 'a * 'b -> 'a * 'b * mixfix |
19271 | 25 |
val pretty_mixfix: mixfix -> Pretty.T |
4823 | 26 |
val mixfix_args: mixfix -> int |
22702 | 27 |
val mixfixT: mixfix -> typ |
2199
bcb360f80dac
added Infixl/rName: specify infix name independently from syntax;
wenzelm
parents:
1952
diff
changeset
|
28 |
end; |
384 | 29 |
|
30 |
signature MIXFIX = |
|
2199
bcb360f80dac
added Infixl/rName: specify infix name independently from syntax;
wenzelm
parents:
1952
diff
changeset
|
31 |
sig |
384 | 32 |
include MIXFIX1 |
14903 | 33 |
val syn_ext_types: (string * int * mixfix) list -> SynExt.syn_ext |
34 |
val syn_ext_consts: (string -> bool) -> (string * typ * mixfix) list -> SynExt.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 |
||
2199
bcb360f80dac
added Infixl/rName: specify infix name independently from syntax;
wenzelm
parents:
1952
diff
changeset
|
40 |
|
384 | 41 |
(** mixfix declarations **) |
42 |
||
43 |
datatype mixfix = |
|
44 |
NoSyn | |
|
45 |
Mixfix of string * int list * int | |
|
46 |
Delimfix of string | |
|
35130 | 47 |
Infix of string * int | |
48 |
Infixl of string * int | |
|
49 |
Infixr of string * int | |
|
18673 | 50 |
Binder of string * int * int | |
51 |
Structure; |
|
384 | 52 |
|
4823 | 53 |
fun no_syn (x, y) = (x, y, NoSyn); |
54 |
||
384 | 55 |
|
19271 | 56 |
(* pretty_mixfix *) |
57 |
||
58 |
local |
|
11920 | 59 |
|
19271 | 60 |
val quoted = Pretty.quote o Pretty.str; |
61 |
val keyword = Pretty.keyword; |
|
62 |
val parens = Pretty.enclose "(" ")"; |
|
63 |
val brackets = Pretty.enclose "[" "]"; |
|
64 |
val int = Pretty.str o string_of_int; |
|
65 |
||
66 |
in |
|
11920 | 67 |
|
19271 | 68 |
fun pretty_mixfix NoSyn = Pretty.str "" |
69 |
| pretty_mixfix (Mixfix (s, ps, p)) = |
|
70 |
parens (Pretty.breaks [quoted s, brackets (Pretty.commas (map int ps)), int p]) |
|
71 |
| pretty_mixfix (Delimfix s) = parens [quoted s] |
|
35130 | 72 |
| pretty_mixfix (Infix (s, p)) = parens (Pretty.breaks [keyword "infix", quoted s, int p]) |
73 |
| pretty_mixfix (Infixl (s, p)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p]) |
|
74 |
| pretty_mixfix (Infixr (s, p)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p]) |
|
19271 | 75 |
| pretty_mixfix (Binder (s, p1, p2)) = |
76 |
parens (Pretty.breaks [keyword "binder", quoted s, brackets [int p1], int p2]) |
|
77 |
| pretty_mixfix Structure = parens [keyword "structure"]; |
|
78 |
||
79 |
end; |
|
11920 | 80 |
|
81 |
||
12531 | 82 |
(* syntax specifications *) |
384 | 83 |
|
4053 | 84 |
fun mixfix_args NoSyn = 0 |
5690 | 85 |
| mixfix_args (Mixfix (sy, _, _)) = SynExt.mfix_args sy |
86 |
| mixfix_args (Delimfix sy) = SynExt.mfix_args sy |
|
35130 | 87 |
| mixfix_args (Infix (sy, _)) = 2 + SynExt.mfix_args sy |
88 |
| mixfix_args (Infixl (sy, _)) = 2 + SynExt.mfix_args sy |
|
89 |
| mixfix_args (Infixr (sy, _)) = 2 + SynExt.mfix_args sy |
|
18673 | 90 |
| mixfix_args (Binder _) = 1 |
91 |
| mixfix_args Structure = 0; |
|
4053 | 92 |
|
22709 | 93 |
fun mixfixT (Binder _) = (dummyT --> dummyT) --> dummyT |
94 |
| mixfixT mx = replicate (mixfix_args mx) dummyT ---> dummyT; |
|
22702 | 95 |
|
4053 | 96 |
|
384 | 97 |
(* syn_ext_types *) |
98 |
||
14903 | 99 |
fun syn_ext_types type_decls = |
384 | 100 |
let |
35351
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
101 |
fun mk_type n = replicate n SynExt.typeT ---> SynExt.typeT; |
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
102 |
fun mk_infix sy n t p1 p2 p3 = SynExt.Mfix ("(_ " ^ sy ^ "/ _)", mk_type n, t, [p1, p2], p3); |
384 | 103 |
|
35129 | 104 |
fun mfix_of (_, _, NoSyn) = NONE |
35351
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
105 |
| mfix_of (t, n, Mixfix (sy, ps, p)) = SOME (SynExt.Mfix (sy, mk_type n, t, ps, p)) |
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
106 |
| mfix_of (t, n, Delimfix sy) = SOME (SynExt.Mfix (sy, mk_type n, t, [], SynExt.max_pri)) |
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
107 |
| mfix_of (t, n, Infix (sy, p)) = SOME (mk_infix sy n t (p + 1) (p + 1) p) |
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
108 |
| mfix_of (t, n, Infixl (sy, p)) = SOME (mk_infix sy n t p (p + 1) p) |
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
109 |
| mfix_of (t, n, Infixr (sy, p)) = SOME (mk_infix sy n t (p + 1) p p) |
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
110 |
| mfix_of (t, _, _) = error ("Bad mixfix declaration for " ^ quote t); (* FIXME printable t (!?) *) |
384 | 111 |
|
35351
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
112 |
fun check_args (t, n, _) (SOME (mfix as SynExt.Mfix (sy, _, _, _, _))) = |
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
113 |
if SynExt.mfix_args sy = n then () |
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
114 |
else SynExt.err_in_mfix "Bad number of type constructor arguments" mfix |
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
115 |
| check_args _ NONE = (); |
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
116 |
|
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
117 |
val mfix = map mfix_of type_decls; |
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
118 |
val _ = map2 check_args type_decls mfix; |
35129 | 119 |
val xconsts = map #1 type_decls; |
35351
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
120 |
in SynExt.syn_ext (map_filter I mfix) xconsts ([], [], [], []) [] ([], []) end; |
384 | 121 |
|
122 |
||
123 |
(* syn_ext_consts *) |
|
124 |
||
15751
65e4790c7914
identify binder translations only once (admits remove);
wenzelm
parents:
15570
diff
changeset
|
125 |
val binder_stamp = stamp (); |
21534
68f805e9db0b
Binder: syntax const is determined by binder_name, not its syntax;
wenzelm
parents:
20892
diff
changeset
|
126 |
val binder_name = suffix "_binder"; |
15751
65e4790c7914
identify binder translations only once (admits remove);
wenzelm
parents:
15570
diff
changeset
|
127 |
|
14903 | 128 |
fun syn_ext_consts is_logtype const_decls = |
384 | 129 |
let |
130 |
fun mk_infix sy ty c p1 p2 p3 = |
|
5690 | 131 |
[SynExt.Mfix ("op " ^ sy, ty, c, [], SynExt.max_pri), |
12512
ab14b29dfc6d
removed special treatment of "_" in syntax (now covered by \<index> arg);
wenzelm
parents:
12149
diff
changeset
|
132 |
SynExt.Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3)]; |
384 | 133 |
|
134 |
fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) = |
|
135 |
[Type ("idts", []), ty2] ---> ty3 |
|
14903 | 136 |
| binder_typ c _ = error ("Bad type of binder: " ^ quote c); |
384 | 137 |
|
35129 | 138 |
fun mfix_of (_, _, NoSyn) = [] |
139 |
| mfix_of (c, ty, Mixfix (sy, ps, p)) = [SynExt.Mfix (sy, ty, c, ps, p)] |
|
140 |
| mfix_of (c, ty, Delimfix sy) = [SynExt.Mfix (sy, ty, c, [], SynExt.max_pri)] |
|
35130 | 141 |
| mfix_of (c, ty, Infix (sy, p)) = mk_infix sy ty c (p + 1) (p + 1) p |
142 |
| mfix_of (c, ty, Infixl (sy, p)) = mk_infix sy ty c p (p + 1) p |
|
143 |
| mfix_of (c, ty, Infixr (sy, p)) = mk_infix sy ty c (p + 1) p p |
|
35129 | 144 |
| mfix_of (c, ty, Binder (sy, p, q)) = |
145 |
[SynExt.Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, (binder_name c), [0, p], q)] |
|
35351
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
146 |
| mfix_of (c, _, _) = error ("Bad mixfix declaration for " ^ quote c); (* FIXME printable c (!?) *) |
384 | 147 |
|
21534
68f805e9db0b
Binder: syntax const is determined by binder_name, not its syntax;
wenzelm
parents:
20892
diff
changeset
|
148 |
fun binder (c, _, Binder _) = SOME (binder_name c, c) |
15531 | 149 |
| binder _ = NONE; |
384 | 150 |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19467
diff
changeset
|
151 |
val mfix = maps mfix_of const_decls; |
35129 | 152 |
val xconsts = map #1 const_decls; |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19467
diff
changeset
|
153 |
val binders = map_filter binder const_decls; |
35129 | 154 |
val binder_trs = binders |
155 |
|> map (SynExt.stamp_trfun binder_stamp o apsnd K o SynTrans.mk_binder_tr); |
|
156 |
val binder_trs' = binders |
|
157 |
|> map (SynExt.stamp_trfun binder_stamp o |
|
158 |
apsnd (K o SynTrans.non_typed_tr') o SynTrans.mk_binder_tr' o swap); |
|
14903 | 159 |
in |
160 |
SynExt.syn_ext' true is_logtype |
|
161 |
mfix xconsts ([], binder_trs, binder_trs', []) [] ([], []) |
|
162 |
end; |
|
384 | 163 |
|
164 |
end; |