author | wenzelm |
Fri, 08 Apr 2011 15:02:11 +0200 | |
changeset 42288 | 2074b31650e6 |
parent 42268 | src/Pure/Syntax/syn_ext.ML@01401287c3f7 |
child 42293 | 6cca0343ea48 |
permissions | -rw-r--r-- |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42268
diff
changeset
|
1 |
(* Title: Pure/Syntax/syntax_ext.ML |
911
55754d6d399c
new in mixfix annotations: "' " (quote space) separates delimiters without
wenzelm
parents:
865
diff
changeset
|
2 |
Author: Markus Wenzel and Carsten Clasohm, TU Muenchen |
240 | 3 |
|
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42268
diff
changeset
|
4 |
Syntax extension. |
240 | 5 |
*) |
6 |
||
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42268
diff
changeset
|
7 |
signature SYNTAX_EXT = |
4050 | 8 |
sig |
6760 | 9 |
val dddot_indexname: indexname |
1510 | 10 |
val logic: string |
11 |
val args: string |
|
12 |
val cargs: string |
|
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42268
diff
changeset
|
13 |
val typeT: typ |
30189
3633f560f4c3
discontinued experimental support for Alice -- too hard to maintain its many language incompatibilities, never really worked anyway;
wenzelm
parents:
29565
diff
changeset
|
14 |
val any: string |
1510 | 15 |
val sprop: string |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42268
diff
changeset
|
16 |
val spropT: typ |
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42268
diff
changeset
|
17 |
val max_pri: int |
35351
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35110
diff
changeset
|
18 |
datatype mfix = Mfix of string * typ * string * int list * int |
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
35110
diff
changeset
|
19 |
val err_in_mfix: string -> mfix -> 'a |
1510 | 20 |
val typ_to_nonterm: typ -> string |
21 |
datatype xsymb = |
|
22 |
Delim of string | |
|
23 |
Argument of string * int | |
|
24 |
Space of string | |
|
25 |
Bg of int | Brk of int | En |
|
26 |
datatype xprod = XProd of string * xsymb list * string * int |
|
27 |
val chain_pri: int |
|
4701 | 28 |
val delims_of: xprod list -> string list list |
1510 | 29 |
datatype syn_ext = |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
35429
diff
changeset
|
30 |
Syn_Ext of { |
1510 | 31 |
xprods: xprod list, |
32 |
consts: string list, |
|
21772 | 33 |
parse_ast_translation: (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list, |
1510 | 34 |
parse_rules: (Ast.ast * Ast.ast) list, |
21772 | 35 |
parse_translation: (string * ((Proof.context -> term list -> term) * stamp)) list, |
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
36 |
print_translation: (string * ((Proof.context -> typ -> term list -> term) * stamp)) list, |
1510 | 37 |
print_rules: (Ast.ast * Ast.ast) list, |
42268 | 38 |
print_ast_translation: (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list} |
19004 | 39 |
val mfix_delims: string -> string list |
4054 | 40 |
val mfix_args: string -> int |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42268
diff
changeset
|
41 |
val escape: string -> string |
14903 | 42 |
val syn_ext': bool -> (string -> bool) -> mfix list -> |
21772 | 43 |
string list -> (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list * |
44 |
(string * ((Proof.context -> term list -> term) * stamp)) list * |
|
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
45 |
(string * ((Proof.context -> typ -> term list -> term) * stamp)) list * |
42268 | 46 |
(string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list -> |
47 |
(Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext |
|
14903 | 48 |
val syn_ext: mfix list -> string list -> |
21772 | 49 |
(string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list * |
50 |
(string * ((Proof.context -> term list -> term) * stamp)) list * |
|
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
51 |
(string * ((Proof.context -> typ -> term list -> term) * stamp)) list * |
42268 | 52 |
(string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list -> |
53 |
(Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext |
|
14903 | 54 |
val syn_ext_const_names: string list -> syn_ext |
55 |
val syn_ext_rules: (Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext |
|
56 |
val syn_ext_trfuns: |
|
15754 | 57 |
(string * ((Ast.ast list -> Ast.ast) * stamp)) list * |
58 |
(string * ((term list -> term) * stamp)) list * |
|
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
59 |
(string * ((typ -> term list -> term) * stamp)) list * |
15754 | 60 |
(string * ((Ast.ast list -> Ast.ast) * stamp)) list -> syn_ext |
16610 | 61 |
val syn_ext_advanced_trfuns: |
21772 | 62 |
(string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list * |
63 |
(string * ((Proof.context -> term list -> term) * stamp)) list * |
|
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
64 |
(string * ((Proof.context -> typ -> term list -> term) * stamp)) list * |
21772 | 65 |
(string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list -> syn_ext |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42268
diff
changeset
|
66 |
val stamp_trfun: stamp -> string * 'a -> string * ('a * stamp) |
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42268
diff
changeset
|
67 |
val mk_trfun: string * 'a -> string * ('a * stamp) |
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42268
diff
changeset
|
68 |
val eq_trfun: ('a * stamp) * ('a * stamp) -> bool |
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42268
diff
changeset
|
69 |
val token_markers: string list |
1510 | 70 |
val pure_ext: syn_ext |
4050 | 71 |
end; |
240 | 72 |
|
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42268
diff
changeset
|
73 |
structure Syntax_Ext: SYNTAX_EXT = |
240 | 74 |
struct |
75 |
||
2694 | 76 |
|
240 | 77 |
(** misc definitions **) |
78 |
||
7472 | 79 |
val dddot_indexname = ("dddot", 0); |
6760 | 80 |
|
81 |
||
240 | 82 |
(* syntactic categories *) |
83 |
||
84 |
val logic = "logic"; |
|
85 |
val logicT = Type (logic, []); |
|
86 |
||
87 |
val args = "args"; |
|
1178 | 88 |
val cargs = "cargs"; |
240 | 89 |
|
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
240
diff
changeset
|
90 |
val typeT = Type ("type", []); |
240 | 91 |
|
764
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
92 |
val sprop = "#prop"; |
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
93 |
val spropT = Type (sprop, []); |
240 | 94 |
|
30189
3633f560f4c3
discontinued experimental support for Alice -- too hard to maintain its many language incompatibilities, never really worked anyway;
wenzelm
parents:
29565
diff
changeset
|
95 |
val any = "any"; |
3633f560f4c3
discontinued experimental support for Alice -- too hard to maintain its many language incompatibilities, never really worked anyway;
wenzelm
parents:
29565
diff
changeset
|
96 |
val anyT = Type (any, []); |
624 | 97 |
|
780 | 98 |
|
240 | 99 |
|
100 |
(** datatype xprod **) |
|
101 |
||
102 |
(*Delim s: delimiter s |
|
103 |
Argument (s, p): nonterminal s requiring priority >= p, or valued token |
|
104 |
Space s: some white space for printing |
|
105 |
Bg, Brk, En: blocks and breaks for pretty printing*) |
|
106 |
||
107 |
datatype xsymb = |
|
108 |
Delim of string | |
|
109 |
Argument of string * int | |
|
110 |
Space of string | |
|
111 |
Bg of int | Brk of int | En; |
|
112 |
||
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
113 |
fun is_delim (Delim _) = true |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
114 |
| is_delim _ = false; |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
115 |
|
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
116 |
fun is_terminal (Delim _) = true |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
117 |
| is_terminal (Argument (s, _)) = Lexicon.is_terminal s |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
118 |
| is_terminal _ = false; |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
119 |
|
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
120 |
fun is_argument (Argument _) = true |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
121 |
| is_argument _ = false; |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
122 |
|
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
123 |
fun is_index (Argument ("index", _)) = true |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
124 |
| is_index _ = false; |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
125 |
|
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
126 |
val index = Argument ("index", 1000); |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
127 |
|
240 | 128 |
|
129 |
(*XProd (lhs, syms, c, p): |
|
130 |
lhs: name of nonterminal on the lhs of the production |
|
131 |
syms: list of symbols on the rhs of the production |
|
132 |
c: head of parse tree |
|
133 |
p: priority of this production*) |
|
134 |
||
135 |
datatype xprod = XProd of string * xsymb list * string * int; |
|
136 |
||
137 |
val max_pri = 1000; (*maximum legal priority*) |
|
138 |
val chain_pri = ~1; (*dummy for chain productions*) |
|
139 |
||
140 |
fun delims_of xprods = |
|
19004 | 141 |
fold (fn XProd (_, xsymbs, _, _) => |
142 |
fold (fn Delim s => insert (op =) s | _ => I) xsymbs) xprods [] |
|
143 |
|> map Symbol.explode; |
|
240 | 144 |
|
145 |
||
146 |
||
147 |
(** datatype mfix **) |
|
148 |
||
149 |
(*Mfix (sy, ty, c, ps, p): |
|
150 |
sy: rhs of production as symbolic string |
|
151 |
ty: type description of production |
|
152 |
c: head of parse tree |
|
153 |
ps: priorities of arguments in sy |
|
154 |
p: priority of production*) |
|
155 |
||
156 |
datatype mfix = Mfix of string * typ * string * int list * int; |
|
157 |
||
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
158 |
fun err_in_mfix msg (Mfix (sy, _, const, _, _)) = |
18678 | 159 |
cat_error msg ("in mixfix annotation " ^ quote sy ^ " for " ^ quote const); |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
160 |
|
240 | 161 |
|
162 |
(* typ_to_nonterm *) |
|
163 |
||
865 | 164 |
fun typ_to_nt _ (Type (c, _)) = c |
165 |
| typ_to_nt default _ = default; |
|
166 |
||
764
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
167 |
(*get nonterminal for rhs*) |
30189
3633f560f4c3
discontinued experimental support for Alice -- too hard to maintain its many language incompatibilities, never really worked anyway;
wenzelm
parents:
29565
diff
changeset
|
168 |
val typ_to_nonterm = typ_to_nt any; |
240 | 169 |
|
764
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
170 |
(*get nonterminal for lhs*) |
865 | 171 |
val typ_to_nonterm1 = typ_to_nt logic; |
240 | 172 |
|
173 |
||
19004 | 174 |
(* read mixfix annotations *) |
4050 | 175 |
|
176 |
local |
|
19004 | 177 |
|
20675 | 178 |
val is_meta = member (op =) ["(", ")", "/", "_", "\\<index>"]; |
19004 | 179 |
|
180 |
val scan_delim_char = |
|
23784
75e6b9dd5336
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23660
diff
changeset
|
181 |
$$ "'" |-- Scan.one ((not o Symbol.is_blank) andf Symbol.is_regular) || |
75e6b9dd5336
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23660
diff
changeset
|
182 |
Scan.one ((not o is_meta) andf (not o Symbol.is_blank) andf Symbol.is_regular); |
19004 | 183 |
|
184 |
fun read_int ["0", "0"] = ~1 |
|
185 |
| read_int cs = #1 (Library.read_int cs); |
|
4050 | 186 |
|
19004 | 187 |
val scan_sym = |
188 |
$$ "_" >> K (Argument ("", 0)) || |
|
189 |
$$ "\\<index>" >> K index || |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21772
diff
changeset
|
190 |
$$ "(" |-- Scan.many Symbol.is_digit >> (Bg o read_int) || |
19004 | 191 |
$$ ")" >> K En || |
192 |
$$ "/" -- $$ "/" >> K (Brk ~1) || |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21772
diff
changeset
|
193 |
$$ "/" |-- Scan.many Symbol.is_blank >> (Brk o length) || |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21772
diff
changeset
|
194 |
Scan.many1 Symbol.is_blank >> (Space o implode) || |
19004 | 195 |
Scan.repeat1 scan_delim_char >> (Delim o implode); |
14819 | 196 |
|
19004 | 197 |
val scan_symb = |
198 |
scan_sym >> SOME || |
|
199 |
$$ "'" -- Scan.one Symbol.is_blank >> K NONE; |
|
200 |
||
19305 | 201 |
val scan_symbs = Scan.repeat scan_symb --| Scan.ahead (~$$ "'"); |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19305
diff
changeset
|
202 |
val read_symbs = map_filter I o the o Scan.read Symbol.stopper scan_symbs; |
4050 | 203 |
|
19004 | 204 |
fun unique_index xsymbs = |
33317 | 205 |
if length (filter is_index xsymbs) <= 1 then xsymbs |
19004 | 206 |
else error "Duplicate index arguments (\\<index>)"; |
4050 | 207 |
|
19004 | 208 |
in |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
209 |
|
19004 | 210 |
val read_mfix = unique_index o read_symbs o Symbol.explode; |
211 |
||
212 |
fun mfix_delims sy = fold_rev (fn Delim s => cons s | _ => I) (read_mfix sy) []; |
|
33317 | 213 |
val mfix_args = length o filter is_argument o read_mfix; |
19004 | 214 |
|
35390 | 215 |
val escape = implode o map (fn s => if is_meta s then "'" ^ s else s) o Symbol.explode; |
19004 | 216 |
|
4050 | 217 |
end; |
218 |
||
219 |
||
240 | 220 |
(* mfix_to_xprod *) |
221 |
||
14903 | 222 |
fun mfix_to_xprod convert is_logtype (mfix as Mfix (sy, typ, const, pris, pri)) = |
240 | 223 |
let |
224 |
fun check_pri p = |
|
225 |
if p >= 0 andalso p <= max_pri then () |
|
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
226 |
else err_in_mfix ("Precedence out of range: " ^ string_of_int p) mfix; |
240 | 227 |
|
228 |
fun blocks_ok [] 0 = true |
|
229 |
| blocks_ok [] _ = false |
|
230 |
| blocks_ok (Bg _ :: syms) n = blocks_ok syms (n + 1) |
|
231 |
| blocks_ok (En :: _) 0 = false |
|
232 |
| blocks_ok (En :: syms) n = blocks_ok syms (n - 1) |
|
233 |
| blocks_ok (_ :: syms) n = blocks_ok syms n; |
|
234 |
||
235 |
fun check_blocks syms = |
|
236 |
if blocks_ok syms 0 then () |
|
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
237 |
else err_in_mfix "Unbalanced block parentheses" mfix; |
240 | 238 |
|
239 |
||
240 |
val cons_fst = apfst o cons; |
|
241 |
||
242 |
fun add_args [] ty [] = ([], typ_to_nonterm1 ty) |
|
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
243 |
| add_args [] _ _ = err_in_mfix "Too many precedences" mfix |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
244 |
| add_args ((arg as Argument ("index", _)) :: syms) ty ps = |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
245 |
cons_fst arg (add_args syms ty ps) |
240 | 246 |
| add_args (Argument _ :: syms) (Type ("fun", [ty, tys])) [] = |
247 |
cons_fst (Argument (typ_to_nonterm ty, 0)) (add_args syms tys []) |
|
248 |
| add_args (Argument _ :: syms) (Type ("fun", [ty, tys])) (p :: ps) = |
|
249 |
cons_fst (Argument (typ_to_nonterm ty, p)) (add_args syms tys ps) |
|
250 |
| add_args (Argument _ :: _) _ _ = |
|
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
251 |
err_in_mfix "More arguments than in corresponding type" mfix |
240 | 252 |
| add_args (sym :: syms) ty ps = cons_fst sym (add_args syms ty ps); |
253 |
||
254 |
fun rem_pri (Argument (s, _)) = Argument (s, chain_pri) |
|
255 |
| rem_pri sym = sym; |
|
256 |
||
32785 | 257 |
fun logify_types (a as (Argument (s, p))) = |
14903 | 258 |
if s <> "prop" andalso is_logtype s then Argument (logic, p) else a |
32785 | 259 |
| logify_types a = a; |
240 | 260 |
|
2364 | 261 |
|
19004 | 262 |
val raw_symbs = read_mfix sy handle ERROR msg => err_in_mfix msg mfix; |
33317 | 263 |
val args = filter (fn Argument _ => true | _ => false) raw_symbs; |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
264 |
val (const', typ', parse_rules) = |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
265 |
if not (exists is_index args) then (const, typ, []) |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
266 |
else |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
267 |
let |
35429
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35390
diff
changeset
|
268 |
val indexed_const = |
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35390
diff
changeset
|
269 |
if const <> "" then const ^ "_indexed" |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
270 |
else err_in_mfix "Missing constant name for indexed syntax" mfix; |
14697 | 271 |
val rangeT = Term.range_type typ handle Match => |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
272 |
err_in_mfix "Missing structure argument for indexed syntax" mfix; |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
273 |
|
20076 | 274 |
val xs = map Ast.Variable (Name.invent_list [] "xa" (length args - 1)); |
19012 | 275 |
val (xs1, xs2) = chop (find_index is_index args) xs; |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
276 |
val i = Ast.Variable "i"; |
14697 | 277 |
val lhs = Ast.mk_appl (Ast.Constant indexed_const) |
278 |
(xs1 @ [Ast.mk_appl (Ast.Constant "_index") [i]] @ xs2); |
|
279 |
val rhs = Ast.mk_appl (Ast.Constant const) (i :: xs); |
|
280 |
in (indexed_const, rangeT, [(lhs, rhs)]) end; |
|
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
281 |
|
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
282 |
val (symbs, lhs) = add_args raw_symbs typ' pris; |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
283 |
|
2364 | 284 |
val copy_prod = |
20675 | 285 |
(lhs = "prop" orelse lhs = "logic") |
2364 | 286 |
andalso const <> "" |
287 |
andalso not (null symbs) |
|
288 |
andalso not (exists is_delim symbs); |
|
289 |
val lhs' = |
|
290 |
if convert andalso not copy_prod then |
|
14903 | 291 |
(if lhs = "prop" then sprop else if is_logtype lhs then logic else lhs) |
2364 | 292 |
else lhs; |
32785 | 293 |
val symbs' = map logify_types symbs; |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
294 |
val xprod = XProd (lhs', symbs', const', pri); |
240 | 295 |
|
15570 | 296 |
val _ = (List.app check_pri pris; check_pri pri; check_blocks symbs'); |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
297 |
val xprod' = |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
298 |
if Lexicon.is_terminal lhs' then err_in_mfix ("Illegal lhs: " ^ lhs') mfix |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
299 |
else if const <> "" then xprod |
33317 | 300 |
else if length (filter is_argument symbs') <> 1 then |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
301 |
err_in_mfix "Copy production must have exactly one argument" mfix |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
302 |
else if exists is_terminal symbs' then xprod |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
303 |
else XProd (lhs', map rem_pri symbs', "", chain_pri); |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
304 |
|
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
305 |
in (xprod', parse_rules) end; |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
306 |
|
240 | 307 |
|
308 |
||
309 |
(** datatype syn_ext **) |
|
310 |
||
311 |
datatype syn_ext = |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
35429
diff
changeset
|
312 |
Syn_Ext of { |
240 | 313 |
xprods: xprod list, |
314 |
consts: string list, |
|
21772 | 315 |
parse_ast_translation: (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list, |
1510 | 316 |
parse_rules: (Ast.ast * Ast.ast) list, |
21772 | 317 |
parse_translation: (string * ((Proof.context -> term list -> term) * stamp)) list, |
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
318 |
print_translation: (string * ((Proof.context -> typ -> term list -> term) * stamp)) list, |
1510 | 319 |
print_rules: (Ast.ast * Ast.ast) list, |
42268 | 320 |
print_ast_translation: (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list}; |
240 | 321 |
|
322 |
||
323 |
(* syn_ext *) |
|
324 |
||
42268 | 325 |
fun syn_ext' convert is_logtype mfixes consts trfuns (parse_rules, print_rules) = |
240 | 326 |
let |
327 |
val (parse_ast_translation, parse_translation, print_translation, |
|
328 |
print_ast_translation) = trfuns; |
|
329 |
||
14903 | 330 |
val (xprods, parse_rules') = map (mfix_to_xprod convert is_logtype) mfixes |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19305
diff
changeset
|
331 |
|> split_list |> apsnd (rev o flat); |
19046
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
wenzelm
parents:
19012
diff
changeset
|
332 |
val mfix_consts = |
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
wenzelm
parents:
19012
diff
changeset
|
333 |
distinct (op =) (map (fn Mfix x => #3 x) mfixes @ map (fn XProd x => #3 x) xprods); |
240 | 334 |
in |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
35429
diff
changeset
|
335 |
Syn_Ext { |
624 | 336 |
xprods = xprods, |
33042 | 337 |
consts = union (op =) consts mfix_consts, |
240 | 338 |
parse_ast_translation = parse_ast_translation, |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
339 |
parse_rules = parse_rules' @ parse_rules, |
240 | 340 |
parse_translation = parse_translation, |
341 |
print_translation = print_translation, |
|
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
342 |
print_rules = map swap parse_rules' @ print_rules, |
42268 | 343 |
print_ast_translation = print_ast_translation} |
240 | 344 |
end; |
345 |
||
2382 | 346 |
|
14903 | 347 |
val syn_ext = syn_ext' true (K false); |
555
a7f397a14b16
removed idT, varT, tidT, tvarT (now in lexicon.ML);
wenzelm
parents:
441
diff
changeset
|
348 |
|
42268 | 349 |
fun syn_ext_const_names cs = syn_ext [] cs ([], [], [], []) ([], []); |
350 |
fun syn_ext_rules rules = syn_ext [] [] ([], [], [], []) rules; |
|
351 |
fun syn_ext_advanced_trfuns trfuns = syn_ext [] [] trfuns ([], []); |
|
2382 | 352 |
|
16610 | 353 |
fun syn_ext_trfuns (atrs, trs, tr's, atr's) = |
354 |
let fun simple (name, (f, s)) = (name, (K f, s)) in |
|
355 |
syn_ext_advanced_trfuns (map simple atrs, map simple trs, map simple tr's, map simple atr's) |
|
356 |
end; |
|
357 |
||
15754 | 358 |
fun stamp_trfun s (c, f) = (c, (f, s)); |
359 |
fun mk_trfun tr = stamp_trfun (stamp ()) tr; |
|
29318
6337d1cb2ba0
added numeral, which supercedes num, xnum, float;
wenzelm
parents:
28904
diff
changeset
|
360 |
fun eq_trfun ((_, s1: stamp), (_, s2)) = s1 = s2; |
15754 | 361 |
|
240 | 362 |
|
42267
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42248
diff
changeset
|
363 |
(* pure_ext *) |
15835
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
wenzelm
parents:
15754
diff
changeset
|
364 |
|
42267
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42248
diff
changeset
|
365 |
val token_markers = |
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42248
diff
changeset
|
366 |
["_tfree", "_tvar", "_free", "_bound", "_var", "_numeral", "_inner_string"]; |
240 | 367 |
|
14903 | 368 |
val pure_ext = syn_ext' false (K false) |
764
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
369 |
[Mfix ("_", spropT --> propT, "", [0], 0), |
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
370 |
Mfix ("_", logicT --> anyT, "", [0], 0), |
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
371 |
Mfix ("_", spropT --> anyT, "", [0], 0), |
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
372 |
Mfix ("'(_')", logicT --> logicT, "", [0], max_pri), |
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
373 |
Mfix ("'(_')", spropT --> spropT, "", [0], max_pri), |
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
374 |
Mfix ("_::_", [logicT, typeT] ---> logicT, "_constrain", [4, 0], 3), |
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
375 |
Mfix ("_::_", [spropT, typeT] ---> spropT, "_constrain", [4, 0], 3)] |
42267
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42248
diff
changeset
|
376 |
token_markers |
764
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
377 |
([], [], [], []) |
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
378 |
([], []); |
240 | 379 |
|
380 |
end; |