author | wenzelm |
Sun, 15 Mar 2015 21:57:10 +0100 | |
changeset 59706 | bf6ca55aae13 |
parent 55763 | 4b3907cb5654 |
child 59841 | 2551ac44150e |
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 | |
|
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 |
|
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 |
19271 | 23 |
val pretty_mixfix: mixfix -> Pretty.T |
4823 | 24 |
val mixfix_args: mixfix -> int |
22702 | 25 |
val mixfixT: mixfix -> typ |
35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
35390
diff
changeset
|
26 |
val make_type: int -> typ |
42287
d98eb048a2e4
discontinued special treatment of structure Mixfix;
wenzelm
parents:
42284
diff
changeset
|
27 |
val binder_name: string -> string |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
28 |
val syn_ext_types: (string * typ * mixfix) list -> Syntax_Ext.syn_ext |
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
29 |
val syn_ext_consts: (string -> bool) -> (string * typ * mixfix) list -> Syntax_Ext.syn_ext |
2199
bcb360f80dac
added Infixl/rName: specify infix name independently from syntax;
wenzelm
parents:
1952
diff
changeset
|
30 |
end; |
bcb360f80dac
added Infixl/rName: specify infix name independently from syntax;
wenzelm
parents:
1952
diff
changeset
|
31 |
|
15751
65e4790c7914
identify binder translations only once (admits remove);
wenzelm
parents:
15570
diff
changeset
|
32 |
structure Mixfix: MIXFIX = |
384 | 33 |
struct |
34 |
||
35 |
(** mixfix declarations **) |
|
36 |
||
37 |
datatype mixfix = |
|
38 |
NoSyn | |
|
39 |
Mixfix of string * int list * int | |
|
40 |
Delimfix of string | |
|
35130 | 41 |
Infix of string * int | |
42 |
Infixl of string * int | |
|
43 |
Infixr of string * int | |
|
18673 | 44 |
Binder of string * int * int | |
45 |
Structure; |
|
384 | 46 |
|
47 |
||
19271 | 48 |
(* pretty_mixfix *) |
49 |
||
50 |
local |
|
11920 | 51 |
|
19271 | 52 |
val quoted = Pretty.quote o Pretty.str; |
55763 | 53 |
val keyword = Pretty.keyword2; |
19271 | 54 |
val parens = Pretty.enclose "(" ")"; |
55 |
val brackets = Pretty.enclose "[" "]"; |
|
56 |
val int = Pretty.str o string_of_int; |
|
57 |
||
58 |
in |
|
11920 | 59 |
|
19271 | 60 |
fun pretty_mixfix NoSyn = Pretty.str "" |
61 |
| pretty_mixfix (Mixfix (s, ps, p)) = |
|
62 |
parens (Pretty.breaks [quoted s, brackets (Pretty.commas (map int ps)), int p]) |
|
63 |
| pretty_mixfix (Delimfix s) = parens [quoted s] |
|
35130 | 64 |
| pretty_mixfix (Infix (s, p)) = parens (Pretty.breaks [keyword "infix", quoted s, int p]) |
65 |
| pretty_mixfix (Infixl (s, p)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p]) |
|
66 |
| pretty_mixfix (Infixr (s, p)) = parens (Pretty.breaks [keyword "infixl", quoted s, int p]) |
|
19271 | 67 |
| pretty_mixfix (Binder (s, p1, p2)) = |
68 |
parens (Pretty.breaks [keyword "binder", quoted s, brackets [int p1], int p2]) |
|
69 |
| pretty_mixfix Structure = parens [keyword "structure"]; |
|
70 |
||
71 |
end; |
|
11920 | 72 |
|
73 |
||
12531 | 74 |
(* syntax specifications *) |
384 | 75 |
|
4053 | 76 |
fun mixfix_args NoSyn = 0 |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
77 |
| mixfix_args (Mixfix (sy, _, _)) = Syntax_Ext.mfix_args sy |
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
78 |
| mixfix_args (Delimfix sy) = Syntax_Ext.mfix_args sy |
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
79 |
| mixfix_args (Infix (sy, _)) = 2 + Syntax_Ext.mfix_args sy |
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
80 |
| mixfix_args (Infixl (sy, _)) = 2 + Syntax_Ext.mfix_args sy |
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
81 |
| mixfix_args (Infixr (sy, _)) = 2 + Syntax_Ext.mfix_args sy |
18673 | 82 |
| mixfix_args (Binder _) = 1 |
83 |
| mixfix_args Structure = 0; |
|
4053 | 84 |
|
22709 | 85 |
fun mixfixT (Binder _) = (dummyT --> dummyT) --> dummyT |
86 |
| mixfixT mx = replicate (mixfix_args mx) dummyT ---> dummyT; |
|
22702 | 87 |
|
4053 | 88 |
|
384 | 89 |
(* syn_ext_types *) |
90 |
||
42293
6cca0343ea48
renamed sprop "prop#" to "prop'" -- proper identifier;
wenzelm
parents:
42288
diff
changeset
|
91 |
val typeT = Type ("type", []); |
6cca0343ea48
renamed sprop "prop#" to "prop'" -- proper identifier;
wenzelm
parents:
42288
diff
changeset
|
92 |
fun make_type n = replicate n typeT ---> typeT; |
35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
35390
diff
changeset
|
93 |
|
14903 | 94 |
fun syn_ext_types type_decls = |
384 | 95 |
let |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
96 |
fun mk_infix sy ty t p1 p2 p3 = Syntax_Ext.Mfix ("(_ " ^ sy ^ "/ _)", ty, t, [p1, p2], p3); |
384 | 97 |
|
35129 | 98 |
fun mfix_of (_, _, NoSyn) = NONE |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
99 |
| mfix_of (t, ty, Mixfix (sy, ps, p)) = SOME (Syntax_Ext.Mfix (sy, ty, t, ps, p)) |
42297
140f283266b7
discontinued Syntax.max_pri, which is not really a symbolic parameter;
wenzelm
parents:
42293
diff
changeset
|
100 |
| mfix_of (t, ty, Delimfix sy) = SOME (Syntax_Ext.Mfix (sy, ty, t, [], 1000)) |
35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
35390
diff
changeset
|
101 |
| mfix_of (t, ty, Infix (sy, p)) = SOME (mk_infix sy ty t (p + 1) (p + 1) p) |
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
35390
diff
changeset
|
102 |
| mfix_of (t, ty, Infixl (sy, p)) = SOME (mk_infix sy ty t p (p + 1) p) |
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
35390
diff
changeset
|
103 |
| 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
|
104 |
| mfix_of (t, _, _) = error ("Bad mixfix declaration for " ^ quote t); |
384 | 105 |
|
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
106 |
fun check_args (_, ty, _) (SOME (mfix as Syntax_Ext.Mfix (sy, _, _, _, _))) = |
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
107 |
if length (Term.binder_types ty) = Syntax_Ext.mfix_args sy then () |
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
108 |
else Syntax_Ext.err_in_mfix "Bad number of type constructor arguments" mfix |
35351
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
109 |
| check_args _ NONE = (); |
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
110 |
|
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
111 |
val mfix = map mfix_of type_decls; |
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35130
diff
changeset
|
112 |
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
|
113 |
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
|
114 |
in Syntax_Ext.syn_ext (map_filter I mfix) consts ([], [], [], []) ([], []) end; |
384 | 115 |
|
116 |
||
117 |
(* syn_ext_consts *) |
|
118 |
||
15751
65e4790c7914
identify binder translations only once (admits remove);
wenzelm
parents:
15570
diff
changeset
|
119 |
val binder_stamp = stamp (); |
21534
68f805e9db0b
Binder: syntax const is determined by binder_name, not its syntax;
wenzelm
parents:
20892
diff
changeset
|
120 |
val binder_name = suffix "_binder"; |
15751
65e4790c7914
identify binder translations only once (admits remove);
wenzelm
parents:
15570
diff
changeset
|
121 |
|
14903 | 122 |
fun syn_ext_consts is_logtype const_decls = |
384 | 123 |
let |
124 |
fun mk_infix sy ty c p1 p2 p3 = |
|
42297
140f283266b7
discontinued Syntax.max_pri, which is not really a symbolic parameter;
wenzelm
parents:
42293
diff
changeset
|
125 |
[Syntax_Ext.Mfix ("op " ^ sy, ty, c, [], 1000), |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
126 |
Syntax_Ext.Mfix ("(_ " ^ sy ^ "/ _)", ty, c, [p1, p2], p3)]; |
384 | 127 |
|
128 |
fun binder_typ _ (Type ("fun", [Type ("fun", [_, ty2]), ty3])) = |
|
129 |
[Type ("idts", []), ty2] ---> ty3 |
|
14903 | 130 |
| binder_typ c _ = error ("Bad type of binder: " ^ quote c); |
384 | 131 |
|
35129 | 132 |
fun mfix_of (_, _, NoSyn) = [] |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
133 |
| mfix_of (c, ty, Mixfix (sy, ps, p)) = [Syntax_Ext.Mfix (sy, ty, c, ps, p)] |
42297
140f283266b7
discontinued Syntax.max_pri, which is not really a symbolic parameter;
wenzelm
parents:
42293
diff
changeset
|
134 |
| mfix_of (c, ty, Delimfix sy) = [Syntax_Ext.Mfix (sy, ty, c, [], 1000)] |
35130 | 135 |
| mfix_of (c, ty, Infix (sy, p)) = mk_infix sy ty c (p + 1) (p + 1) p |
136 |
| mfix_of (c, ty, Infixl (sy, p)) = mk_infix sy ty c p (p + 1) p |
|
137 |
| mfix_of (c, ty, Infixr (sy, p)) = mk_infix sy ty c (p + 1) p p |
|
35129 | 138 |
| mfix_of (c, ty, Binder (sy, p, q)) = |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
139 |
[Syntax_Ext.Mfix ("(3" ^ sy ^ "_./ _)", binder_typ c ty, (binder_name c), [0, p], q)] |
42130
e10e2cce85c8
removed unclear comments stemming from ed24ba6f69aa;
wenzelm
parents:
37216
diff
changeset
|
140 |
| mfix_of (c, _, _) = error ("Bad mixfix declaration for " ^ quote c); |
384 | 141 |
|
21534
68f805e9db0b
Binder: syntax const is determined by binder_name, not its syntax;
wenzelm
parents:
20892
diff
changeset
|
142 |
fun binder (c, _, Binder _) = SOME (binder_name c, c) |
15531 | 143 |
| binder _ = NONE; |
384 | 144 |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19467
diff
changeset
|
145 |
val mfix = maps mfix_of const_decls; |
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19467
diff
changeset
|
146 |
val binders = map_filter binder const_decls; |
35129 | 147 |
val binder_trs = binders |
52143 | 148 |
|> map (Syntax_Ext.stamp_trfun binder_stamp o Syntax_Trans.mk_binder_tr); |
35129 | 149 |
val binder_trs' = binders |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
150 |
|> map (Syntax_Ext.stamp_trfun binder_stamp o |
52143 | 151 |
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
|
152 |
|
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
153 |
val consts = binders @ map (fn (c, _, _) => (c, "")) const_decls; |
14903 | 154 |
in |
42298
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
155 |
Syntax_Ext.syn_ext' is_logtype mfix consts ([], binder_trs, binder_trs', []) ([], []) |
14903 | 156 |
end; |
384 | 157 |
|
158 |
end; |
|
42287
d98eb048a2e4
discontinued special treatment of structure Mixfix;
wenzelm
parents:
42284
diff
changeset
|
159 |
|
d98eb048a2e4
discontinued special treatment of structure Mixfix;
wenzelm
parents:
42284
diff
changeset
|
160 |
structure Basic_Mixfix: BASIC_MIXFIX = Mixfix; |
d98eb048a2e4
discontinued special treatment of structure Mixfix;
wenzelm
parents:
42284
diff
changeset
|
161 |
open Basic_Mixfix; |
d98eb048a2e4
discontinued special treatment of structure Mixfix;
wenzelm
parents:
42284
diff
changeset
|
162 |