author | wenzelm |
Sun, 27 Jan 2008 22:21:35 +0100 | |
changeset 25993 | d542486e39e7 |
parent 25476 | 03da46cfab9e |
child 26678 | a3ae088dc20f |
permissions | -rw-r--r-- |
18 | 1 |
(* Title: Pure/Syntax/syntax.ML |
0 | 2 |
ID: $Id$ |
3 |
Author: Tobias Nipkow and Markus Wenzel, TU Muenchen |
|
18 | 4 |
|
24263 | 5 |
Standard Isabelle syntax, based on arbitrary context-free grammars |
6 |
(specified by mixfix declarations). |
|
0 | 7 |
*) |
8 |
||
260 | 9 |
signature BASIC_SYNTAX = |
2383 | 10 |
sig |
260 | 11 |
include AST0 |
556 | 12 |
include SYN_TRANS0 |
13 |
include MIXFIX0 |
|
260 | 14 |
include PRINTER0 |
2383 | 15 |
end; |
260 | 16 |
|
0 | 17 |
signature SYNTAX = |
2383 | 18 |
sig |
260 | 19 |
include AST1 |
0 | 20 |
include LEXICON0 |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
21 |
include SYN_EXT0 |
0 | 22 |
include TYPE_EXT0 |
556 | 23 |
include SYN_TRANS1 |
24 |
include MIXFIX1 |
|
0 | 25 |
include PRINTER0 |
1158 | 26 |
datatype 'a trrule = |
3526 | 27 |
ParseRule of 'a * 'a | |
28 |
PrintRule of 'a * 'a | |
|
29 |
ParsePrintRule of 'a * 'a |
|
556 | 30 |
type syntax |
17079 | 31 |
val eq_syntax: syntax * syntax -> bool |
14687 | 32 |
val is_keyword: syntax -> string -> bool |
20784 | 33 |
type mode |
24970 | 34 |
val mode_default: mode |
35 |
val mode_input: mode |
|
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
36 |
val update_type_gram: (string * int * mixfix) list -> syntax -> syntax |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
37 |
val update_consts: string list -> syntax -> syntax |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
38 |
val update_trfuns: |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
39 |
(string * ((ast list -> ast) * stamp)) list * |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
40 |
(string * ((term list -> term) * stamp)) list * |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
41 |
(string * ((bool -> typ -> term list -> term) * stamp)) list * |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
42 |
(string * ((ast list -> ast) * stamp)) list -> syntax -> syntax |
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
43 |
val update_advanced_trfuns: |
21772 | 44 |
(string * ((Proof.context -> ast list -> ast) * stamp)) list * |
45 |
(string * ((Proof.context -> term list -> term) * stamp)) list * |
|
46 |
(string * ((Proof.context -> bool -> typ -> term list -> term) * stamp)) list * |
|
47 |
(string * ((Proof.context -> ast list -> ast) * stamp)) list -> syntax -> syntax |
|
23660
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23655
diff
changeset
|
48 |
val extend_tokentrfuns: (string * string * (string -> output * int)) list -> syntax -> syntax |
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
49 |
val update_const_gram: (string -> bool) -> |
25387 | 50 |
mode -> (string * typ * mixfix) list -> syntax -> syntax |
19262 | 51 |
val remove_const_gram: (string -> bool) -> |
20784 | 52 |
mode -> (string * typ * mixfix) list -> syntax -> syntax |
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
53 |
val update_trrules: Proof.context -> (string -> bool) -> syntax -> |
14904
7d8dc92fcb7f
removed separate logtypes field of syntax; removed test_read, simple_str_of_sort, simple_string_of_typ; provide default_mode;
wenzelm
parents:
14798
diff
changeset
|
54 |
(string * string) trrule list -> syntax -> syntax |
21772 | 55 |
val remove_trrules: Proof.context -> (string -> bool) -> syntax -> |
19262 | 56 |
(string * string) trrule list -> syntax -> syntax |
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
57 |
val update_trrules_i: ast trrule list -> syntax -> syntax |
19262 | 58 |
val remove_trrules_i: ast trrule list -> syntax -> syntax |
4618 | 59 |
val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule |
556 | 60 |
val merge_syntaxes: syntax -> syntax -> syntax |
18720 | 61 |
val basic_syn: syntax |
62 |
val basic_nonterms: string list |
|
556 | 63 |
val print_gram: syntax -> unit |
64 |
val print_trans: syntax -> unit |
|
65 |
val print_syntax: syntax -> unit |
|
21772 | 66 |
val read: Proof.context -> (string -> bool) -> syntax -> typ -> string -> term list |
25476
03da46cfab9e
standard_parse_term: check ambiguous results without changing the result yet;
wenzelm
parents:
25394
diff
changeset
|
67 |
val standard_parse_term: Pretty.pp -> (term -> string option) -> |
22703 | 68 |
(((string * int) * sort) list -> string * int -> Term.sort) -> |
69 |
(string -> string option) -> (string -> string option) -> |
|
70 |
(typ -> typ) -> (sort -> sort) -> Proof.context -> |
|
24372
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
71 |
(string -> bool) -> syntax -> typ -> string -> term |
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
72 |
val standard_parse_typ: Proof.context -> syntax -> |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
73 |
((indexname * sort) list -> indexname -> sort) -> (sort -> sort) -> string -> typ |
24278 | 74 |
val standard_parse_sort: Proof.context -> syntax -> (sort -> sort) -> string -> sort |
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
75 |
val standard_unparse_term: (string -> xstring) -> |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
76 |
Proof.context -> syntax -> bool -> term -> Pretty.T |
24923 | 77 |
val standard_unparse_typ: Proof.context -> syntax -> typ -> Pretty.T |
78 |
val standard_unparse_sort: Proof.context -> syntax -> sort -> Pretty.T |
|
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
79 |
val ambiguity_is_error: bool ref |
882
b118d1ea0dfd
moved ambiguity_level from sign.ML to Syntax/syntax.ML
clasohm
parents:
865
diff
changeset
|
80 |
val ambiguity_level: int ref |
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
81 |
val ambiguity_limit: int ref |
24263 | 82 |
val parse_sort: Proof.context -> string -> sort |
83 |
val parse_typ: Proof.context -> string -> typ |
|
84 |
val parse_term: Proof.context -> string -> term |
|
85 |
val parse_prop: Proof.context -> string -> term |
|
24768 | 86 |
val unparse_sort: Proof.context -> sort -> Pretty.T |
24923 | 87 |
val unparse_classrel: Proof.context -> class list -> Pretty.T |
88 |
val unparse_arity: Proof.context -> arity -> Pretty.T |
|
24768 | 89 |
val unparse_typ: Proof.context -> typ -> Pretty.T |
90 |
val unparse_term: Proof.context -> term -> Pretty.T |
|
91 |
val install_operations: |
|
92 |
{parse_sort: Proof.context -> string -> sort, |
|
93 |
parse_typ: Proof.context -> string -> typ, |
|
94 |
parse_term: Proof.context -> string -> term, |
|
95 |
parse_prop: Proof.context -> string -> term, |
|
96 |
unparse_sort: Proof.context -> sort -> Pretty.T, |
|
97 |
unparse_typ: Proof.context -> typ -> Pretty.T, |
|
98 |
unparse_term: Proof.context -> term -> Pretty.T} -> unit |
|
99 |
val print_checks: Proof.context -> unit |
|
100 |
val add_typ_check: int -> string -> |
|
25060 | 101 |
(typ list -> Proof.context -> (typ list * Proof.context) option) -> |
24768 | 102 |
Context.generic -> Context.generic |
103 |
val add_term_check: int -> string -> |
|
25060 | 104 |
(term list -> Proof.context -> (term list * Proof.context) option) -> |
24768 | 105 |
Context.generic -> Context.generic |
106 |
val add_typ_uncheck: int -> string -> |
|
25060 | 107 |
(typ list -> Proof.context -> (typ list * Proof.context) option) -> |
24768 | 108 |
Context.generic -> Context.generic |
109 |
val add_term_uncheck: int -> string -> |
|
25060 | 110 |
(term list -> Proof.context -> (term list * Proof.context) option) -> |
24768 | 111 |
Context.generic -> Context.generic |
24488 | 112 |
val check_sort: Proof.context -> sort -> sort |
24512 | 113 |
val check_typ: Proof.context -> typ -> typ |
114 |
val check_term: Proof.context -> term -> term |
|
115 |
val check_prop: Proof.context -> term -> term |
|
24488 | 116 |
val check_typs: Proof.context -> typ list -> typ list |
24263 | 117 |
val check_terms: Proof.context -> term list -> term list |
118 |
val check_props: Proof.context -> term list -> term list |
|
24923 | 119 |
val uncheck_sort: Proof.context -> sort -> sort |
120 |
val uncheck_arity: Proof.context -> arity -> arity |
|
121 |
val uncheck_classrel: Proof.context -> class list -> class list |
|
24768 | 122 |
val uncheck_typs: Proof.context -> typ list -> typ list |
123 |
val uncheck_terms: Proof.context -> term list -> term list |
|
24263 | 124 |
val read_sort: Proof.context -> string -> sort |
125 |
val read_typ: Proof.context -> string -> typ |
|
24488 | 126 |
val read_term: Proof.context -> string -> term |
127 |
val read_prop: Proof.context -> string -> term |
|
24263 | 128 |
val read_terms: Proof.context -> string list -> term list |
129 |
val read_props: Proof.context -> string list -> term list |
|
24709 | 130 |
val read_sort_global: theory -> string -> sort |
131 |
val read_typ_global: theory -> string -> typ |
|
132 |
val read_term_global: theory -> string -> term |
|
133 |
val read_prop_global: theory -> string -> term |
|
24923 | 134 |
val pretty_term: Proof.context -> term -> Pretty.T |
135 |
val pretty_typ: Proof.context -> typ -> Pretty.T |
|
136 |
val pretty_sort: Proof.context -> sort -> Pretty.T |
|
137 |
val pretty_classrel: Proof.context -> class list -> Pretty.T |
|
138 |
val pretty_arity: Proof.context -> arity -> Pretty.T |
|
139 |
val string_of_term: Proof.context -> term -> string |
|
140 |
val string_of_typ: Proof.context -> typ -> string |
|
141 |
val string_of_sort: Proof.context -> sort -> string |
|
142 |
val string_of_classrel: Proof.context -> class list -> string |
|
143 |
val string_of_arity: Proof.context -> arity -> string |
|
24970 | 144 |
val pp: Proof.context -> Pretty.pp |
2383 | 145 |
end; |
0 | 146 |
|
12094 | 147 |
structure Syntax: SYNTAX = |
0 | 148 |
struct |
149 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
150 |
(** tables of translation functions **) |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
151 |
|
5692 | 152 |
(* parse (ast) translations *) |
153 |
||
17412 | 154 |
fun lookup_tr tab c = Option.map fst (Symtab.lookup tab c); |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
155 |
|
23655
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23631
diff
changeset
|
156 |
fun err_dup_trfun name c = |
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23631
diff
changeset
|
157 |
error ("More than one " ^ name ^ " for " ^ quote c); |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
158 |
|
21536
f119c730f509
extend_trtab: allow identical trfuns to be overwritten;
wenzelm
parents:
20784
diff
changeset
|
159 |
fun remove_trtab trfuns = fold (Symtab.remove SynExt.eq_trfun) trfuns; |
f119c730f509
extend_trtab: allow identical trfuns to be overwritten;
wenzelm
parents:
20784
diff
changeset
|
160 |
|
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
161 |
fun update_trtab name trfuns tab = Symtab.extend (remove_trtab trfuns tab, trfuns) |
23655
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23631
diff
changeset
|
162 |
handle Symtab.DUP c => err_dup_trfun name c; |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
163 |
|
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
164 |
fun merge_trtabs name tab1 tab2 = Symtab.merge SynExt.eq_trfun (tab1, tab2) |
23655
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23631
diff
changeset
|
165 |
handle Symtab.DUP c => err_dup_trfun name c; |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
166 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
167 |
|
5692 | 168 |
(* print (ast) translations *) |
169 |
||
18931 | 170 |
fun lookup_tr' tab c = map fst (Symtab.lookup_list tab c); |
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
171 |
fun update_tr'tab trfuns = fold_rev (Symtab.update_list SynExt.eq_trfun) trfuns; |
18931 | 172 |
fun remove_tr'tab trfuns = fold (Symtab.remove_list SynExt.eq_trfun) trfuns; |
173 |
fun merge_tr'tabs tab1 tab2 = Symtab.merge_list SynExt.eq_trfun (tab1, tab2); |
|
5692 | 174 |
|
175 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
176 |
|
2700 | 177 |
(** tables of token translation functions **) |
178 |
||
179 |
fun lookup_tokentr tabs modes = |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19375
diff
changeset
|
180 |
let val trs = distinct (eq_fst (op =)) (maps (these o AList.lookup (op =) tabs) (modes @ [""])) |
17314 | 181 |
in fn c => Option.map fst (AList.lookup (op =) trs c) end; |
2700 | 182 |
|
183 |
fun merge_tokentrtabs tabs1 tabs2 = |
|
184 |
let |
|
185 |
fun eq_tr ((c1, (_, s1)), (c2, (_, s2))) = c1 = c2 andalso s1 = s2; |
|
186 |
||
4703 | 187 |
fun name (s, _) = implode (tl (Symbol.explode s)); |
2706 | 188 |
|
2700 | 189 |
fun merge mode = |
190 |
let |
|
17213 | 191 |
val trs1 = these (AList.lookup (op =) tabs1 mode); |
192 |
val trs2 = these (AList.lookup (op =) tabs2 mode); |
|
19046
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
wenzelm
parents:
18977
diff
changeset
|
193 |
val trs = distinct eq_tr (trs1 @ trs2); |
2700 | 194 |
in |
18964 | 195 |
(case duplicates (eq_fst (op =)) trs of |
2700 | 196 |
[] => (mode, trs) |
197 |
| dups => error ("More than one token translation function in mode " ^ |
|
2706 | 198 |
quote mode ^ " for " ^ commas_quote (map name dups))) |
2700 | 199 |
end; |
19046
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
wenzelm
parents:
18977
diff
changeset
|
200 |
in map merge (distinct (op =) (map fst (tabs1 @ tabs2))) end; |
2700 | 201 |
|
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
202 |
fun extend_tokentrtab tokentrs tabs = |
2700 | 203 |
let |
17213 | 204 |
fun ins_tokentr (m, c, f) = |
205 |
AList.default (op =) (m, []) |
|
206 |
#> AList.map_entry (op =) m (cons ("_" ^ c, (f, stamp ()))); |
|
15759 | 207 |
in merge_tokentrtabs tabs (fold ins_tokentr tokentrs []) end; |
2700 | 208 |
|
209 |
||
210 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
211 |
(** tables of translation rules **) |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
212 |
|
5692 | 213 |
type ruletab = (Ast.ast * Ast.ast) list Symtab.table; |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
214 |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19375
diff
changeset
|
215 |
fun dest_ruletab tab = maps snd (Symtab.dest tab); |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
216 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
217 |
|
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
218 |
(* empty, update, merge ruletabs *) |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
219 |
|
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
220 |
val update_ruletab = fold_rev (fn r => Symtab.update_list (op =) (Ast.head_of_rule r, r)); |
18931 | 221 |
val remove_ruletab = fold (fn r => Symtab.remove_list (op =) (Ast.head_of_rule r, r)); |
222 |
fun merge_ruletabs tab1 tab2 = Symtab.merge_list (op =) (tab1, tab2); |
|
0 | 223 |
|
224 |
||
225 |
||
226 |
(** datatype syntax **) |
|
227 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
228 |
datatype syntax = |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
229 |
Syntax of { |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
230 |
input: SynExt.xprod list, |
4703 | 231 |
lexicon: Scan.lexicon, |
5692 | 232 |
gram: Parser.gram, |
18 | 233 |
consts: string list, |
2913 | 234 |
prmodes: string list, |
21772 | 235 |
parse_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) Symtab.table, |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
236 |
parse_ruletab: ruletab, |
21772 | 237 |
parse_trtab: ((Proof.context -> term list -> term) * stamp) Symtab.table, |
238 |
print_trtab: ((Proof.context -> bool -> typ -> term list -> term) * stamp) list Symtab.table, |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
239 |
print_ruletab: ruletab, |
21772 | 240 |
print_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) list Symtab.table, |
23660
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23655
diff
changeset
|
241 |
tokentrtab: (string * (string * ((string -> output * int) * stamp)) list) list, |
17079 | 242 |
prtabs: Printer.prtabs} * stamp; |
0 | 243 |
|
17079 | 244 |
fun eq_syntax (Syntax (_, s1), Syntax (_, s2)) = s1 = s2; |
245 |
||
246 |
fun is_keyword (Syntax ({lexicon, ...}, _)) = Scan.is_literal lexicon o Symbol.explode; |
|
14687 | 247 |
|
20784 | 248 |
type mode = string * bool; |
24970 | 249 |
val mode_default = ("", true); |
25122 | 250 |
val mode_input = (PrintMode.input, true); |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
251 |
|
18 | 252 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
253 |
(* empty_syntax *) |
18 | 254 |
|
17079 | 255 |
val empty_syntax = Syntax |
256 |
({input = [], |
|
4703 | 257 |
lexicon = Scan.empty_lexicon, |
5692 | 258 |
gram = Parser.empty_gram, |
167 | 259 |
consts = [], |
2913 | 260 |
prmodes = [], |
5692 | 261 |
parse_ast_trtab = Symtab.empty, |
262 |
parse_ruletab = Symtab.empty, |
|
263 |
parse_trtab = Symtab.empty, |
|
264 |
print_trtab = Symtab.empty, |
|
265 |
print_ruletab = Symtab.empty, |
|
266 |
print_ast_trtab = Symtab.empty, |
|
2700 | 267 |
tokentrtab = [], |
17079 | 268 |
prtabs = Printer.empty_prtabs}, stamp ()); |
167 | 269 |
|
270 |
||
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
271 |
(* update_syntax *) |
167 | 272 |
|
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
273 |
fun update_syntax (mode, inout) syn_ext (Syntax (tabs, _)) = |
167 | 274 |
let |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
275 |
val {input, lexicon, gram, consts = consts1, prmodes = prmodes1, |
2366 | 276 |
parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab, |
2700 | 277 |
print_ast_trtab, tokentrtab, prtabs} = tabs; |
14904
7d8dc92fcb7f
removed separate logtypes field of syntax; removed test_read, simple_str_of_sort, simple_string_of_typ; provide default_mode;
wenzelm
parents:
14798
diff
changeset
|
278 |
val SynExt.SynExt {xprods, consts = consts2, prmodes = prmodes2, |
2913 | 279 |
parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules, |
2700 | 280 |
print_ast_translation, token_translation} = syn_ext; |
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
281 |
val input' = if inout then fold (insert (op =)) xprods input else input; |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
282 |
val changed = length input <> length input'; |
19546
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents:
19482
diff
changeset
|
283 |
fun if_inout xs = if inout then xs else []; |
167 | 284 |
in |
17079 | 285 |
Syntax |
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
286 |
({input = input', |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
287 |
lexicon = if changed then Scan.extend_lexicon (SynExt.delims_of xprods) lexicon else lexicon, |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
288 |
gram = if changed then Parser.extend_gram gram xprods else gram, |
19546
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents:
19482
diff
changeset
|
289 |
consts = Library.merge (op =) (consts1, filter_out NameSpace.is_qualified consts2), |
18921 | 290 |
prmodes = insert (op =) mode (Library.merge (op =) (prmodes1, prmodes2)), |
167 | 291 |
parse_ast_trtab = |
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
292 |
update_trtab "parse ast translation" (if_inout parse_ast_translation) parse_ast_trtab, |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
293 |
parse_ruletab = update_ruletab (if_inout parse_rules) parse_ruletab, |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
294 |
parse_trtab = update_trtab "parse translation" (if_inout parse_translation) parse_trtab, |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
295 |
print_trtab = update_tr'tab print_translation print_trtab, |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
296 |
print_ruletab = update_ruletab print_rules print_ruletab, |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
297 |
print_ast_trtab = update_tr'tab print_ast_translation print_ast_trtab, |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
298 |
tokentrtab = extend_tokentrtab token_translation tokentrtab, |
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
299 |
prtabs = Printer.update_prtabs mode xprods prtabs}, stamp ()) |
18 | 300 |
end; |
301 |
||
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
302 |
|
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
303 |
(* remove_syntax *) |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
304 |
|
17079 | 305 |
fun remove_syntax (mode, inout) syn_ext (Syntax (tabs, _)) = |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
306 |
let |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
307 |
val SynExt.SynExt {xprods, consts = _, prmodes = _, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
308 |
parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
309 |
print_ast_translation, token_translation = _} = syn_ext; |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
310 |
val {input, lexicon, gram, consts, prmodes, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
311 |
parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
312 |
print_ast_trtab, tokentrtab, prtabs} = tabs; |
19300 | 313 |
val input' = if inout then subtract (op =) xprods input else input; |
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
314 |
val changed = length input <> length input'; |
19546
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents:
19482
diff
changeset
|
315 |
fun if_inout xs = if inout then xs else []; |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
316 |
in |
17079 | 317 |
Syntax |
318 |
({input = input', |
|
25387 | 319 |
lexicon = if changed then Scan.make_lexicon (SynExt.delims_of input') else lexicon, |
320 |
gram = if changed then Parser.make_gram input' else gram, |
|
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
321 |
consts = consts, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
322 |
prmodes = prmodes, |
19546
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents:
19482
diff
changeset
|
323 |
parse_ast_trtab = remove_trtab (if_inout parse_ast_translation) parse_ast_trtab, |
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents:
19482
diff
changeset
|
324 |
parse_ruletab = remove_ruletab (if_inout parse_rules) parse_ruletab, |
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents:
19482
diff
changeset
|
325 |
parse_trtab = remove_trtab (if_inout parse_translation) parse_trtab, |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
326 |
print_trtab = remove_tr'tab print_translation print_trtab, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
327 |
print_ruletab = remove_ruletab print_rules print_ruletab, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
328 |
print_ast_trtab = remove_tr'tab print_ast_translation print_ast_trtab, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
329 |
tokentrtab = tokentrtab, |
17079 | 330 |
prtabs = Printer.remove_prtabs mode xprods prtabs}, stamp ()) |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
331 |
end; |
14904
7d8dc92fcb7f
removed separate logtypes field of syntax; removed test_read, simple_str_of_sort, simple_string_of_typ; provide default_mode;
wenzelm
parents:
14798
diff
changeset
|
332 |
|
18 | 333 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
334 |
(* merge_syntaxes *) |
0 | 335 |
|
17079 | 336 |
fun merge_syntaxes (Syntax (tabs1, _)) (Syntax (tabs2, _)) = |
0 | 337 |
let |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
338 |
val {input = input1, lexicon = lexicon1, gram = gram1, consts = consts1, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
339 |
prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
340 |
parse_ruletab = parse_ruletab1, parse_trtab = parse_trtab1, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
341 |
print_trtab = print_trtab1, print_ruletab = print_ruletab1, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
342 |
print_ast_trtab = print_ast_trtab1, tokentrtab = tokentrtab1, prtabs = prtabs1} = tabs1; |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
343 |
|
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
344 |
val {input = input2, lexicon = lexicon2, gram = gram2, consts = consts2, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
345 |
prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
346 |
parse_ruletab = parse_ruletab2, parse_trtab = parse_trtab2, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
347 |
print_trtab = print_trtab2, print_ruletab = print_ruletab2, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
348 |
print_ast_trtab = print_ast_trtab2, tokentrtab = tokentrtab2, prtabs = prtabs2} = tabs2; |
0 | 349 |
in |
17079 | 350 |
Syntax |
18921 | 351 |
({input = Library.merge (op =) (input1, input2), |
4703 | 352 |
lexicon = Scan.merge_lexicons lexicon1 lexicon2, |
5692 | 353 |
gram = Parser.merge_grams gram1 gram2, |
18428 | 354 |
consts = sort_distinct string_ord (consts1 @ consts2), |
18921 | 355 |
prmodes = Library.merge (op =) (prmodes1, prmodes2), |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
356 |
parse_ast_trtab = |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
357 |
merge_trtabs "parse ast translation" parse_ast_trtab1 parse_ast_trtab2, |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
358 |
parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2, |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
359 |
parse_trtab = merge_trtabs "parse translation" parse_trtab1 parse_trtab2, |
5692 | 360 |
print_trtab = merge_tr'tabs print_trtab1 print_trtab2, |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
361 |
print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2, |
5692 | 362 |
print_ast_trtab = merge_tr'tabs print_ast_trtab1 print_ast_trtab2, |
2700 | 363 |
tokentrtab = merge_tokentrtabs tokentrtab1 tokentrtab2, |
17079 | 364 |
prtabs = Printer.merge_prtabs prtabs1 prtabs2}, stamp ()) |
0 | 365 |
end; |
366 |
||
367 |
||
18720 | 368 |
(* basic syntax *) |
260 | 369 |
|
18720 | 370 |
val basic_syn = |
17168 | 371 |
empty_syntax |
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
372 |
|> update_syntax mode_default TypeExt.type_ext |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
373 |
|> update_syntax mode_default SynExt.pure_ext; |
260 | 374 |
|
18720 | 375 |
val basic_nonterms = |
376 |
(Lexicon.terminals @ [SynExt.logic, "type", "types", "sort", "classes", |
|
377 |
SynExt.args, SynExt.cargs, "pttrn", "pttrns", "idt", "idts", "aprop", |
|
22573 | 378 |
"asms", SynExt.any_, SynExt.sprop, "num_const", "index", "struct"]); |
18720 | 379 |
|
0 | 380 |
|
4887 | 381 |
|
15759 | 382 |
(** print syntax **) |
383 |
||
384 |
local |
|
0 | 385 |
|
260 | 386 |
fun pretty_strs_qs name strs = |
14598
7009f59711e3
Replaced quote by Library.quote, since quote now refers to Symbol.quote
berghofe
parents:
14177
diff
changeset
|
387 |
Pretty.strs (name :: map Library.quote (sort_strings strs)); |
0 | 388 |
|
17079 | 389 |
fun pretty_gram (Syntax (tabs, _)) = |
0 | 390 |
let |
14904
7d8dc92fcb7f
removed separate logtypes field of syntax; removed test_read, simple_str_of_sort, simple_string_of_typ; provide default_mode;
wenzelm
parents:
14798
diff
changeset
|
391 |
val {lexicon, prmodes, gram, prtabs, ...} = tabs; |
2913 | 392 |
val prmodes' = sort_strings (filter_out (equal "") prmodes); |
0 | 393 |
in |
8720 | 394 |
[pretty_strs_qs "lexicon:" (Scan.dest_lexicon lexicon), |
395 |
Pretty.big_list "prods:" (Parser.pretty_gram gram), |
|
396 |
pretty_strs_qs "print modes:" prmodes'] |
|
0 | 397 |
end; |
398 |
||
17079 | 399 |
fun pretty_trans (Syntax (tabs, _)) = |
0 | 400 |
let |
260 | 401 |
fun pretty_trtab name tab = |
5692 | 402 |
pretty_strs_qs name (Symtab.keys tab); |
0 | 403 |
|
260 | 404 |
fun pretty_ruletab name tab = |
5692 | 405 |
Pretty.big_list name (map Ast.pretty_rule (dest_ruletab tab)); |
0 | 406 |
|
14598
7009f59711e3
Replaced quote by Library.quote, since quote now refers to Symbol.quote
berghofe
parents:
14177
diff
changeset
|
407 |
fun pretty_tokentr (mode, trs) = Pretty.strs (Library.quote mode ^ ":" :: map fst trs); |
4703 | 408 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
409 |
val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, |
4703 | 410 |
print_ruletab, print_ast_trtab, tokentrtab, ...} = tabs; |
0 | 411 |
in |
8720 | 412 |
[pretty_strs_qs "consts:" consts, |
413 |
pretty_trtab "parse_ast_translation:" parse_ast_trtab, |
|
414 |
pretty_ruletab "parse_rules:" parse_ruletab, |
|
415 |
pretty_trtab "parse_translation:" parse_trtab, |
|
416 |
pretty_trtab "print_translation:" print_trtab, |
|
417 |
pretty_ruletab "print_rules:" print_ruletab, |
|
418 |
pretty_trtab "print_ast_translation:" print_ast_trtab, |
|
419 |
Pretty.big_list "token_translation:" (map pretty_tokentr tokentrtab)] |
|
0 | 420 |
end; |
421 |
||
15759 | 422 |
in |
0 | 423 |
|
15759 | 424 |
fun print_gram syn = Pretty.writeln (Pretty.chunks (pretty_gram syn)); |
425 |
fun print_trans syn = Pretty.writeln (Pretty.chunks (pretty_trans syn)); |
|
426 |
fun print_syntax syn = Pretty.writeln (Pretty.chunks (pretty_gram syn @ pretty_trans syn)); |
|
0 | 427 |
|
15759 | 428 |
end; |
0 | 429 |
|
430 |
||
431 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
432 |
(** read **) |
18 | 433 |
|
260 | 434 |
(* read_ast *) |
435 |
||
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
436 |
val ambiguity_is_error = ref false; |
882
b118d1ea0dfd
moved ambiguity_level from sign.ML to Syntax/syntax.ML
clasohm
parents:
865
diff
changeset
|
437 |
val ambiguity_level = ref 1; |
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
438 |
val ambiguity_limit = ref 10; |
882
b118d1ea0dfd
moved ambiguity_level from sign.ML to Syntax/syntax.ML
clasohm
parents:
865
diff
changeset
|
439 |
|
21772 | 440 |
fun read_asts ctxt is_logtype (Syntax (tabs, _)) xids root str = |
260 | 441 |
let |
14904
7d8dc92fcb7f
removed separate logtypes field of syntax; removed test_read, simple_str_of_sort, simple_string_of_typ; provide default_mode;
wenzelm
parents:
14798
diff
changeset
|
442 |
val {lexicon, gram, parse_ast_trtab, ...} = tabs; |
7d8dc92fcb7f
removed separate logtypes field of syntax; removed test_read, simple_str_of_sort, simple_string_of_typ; provide default_mode;
wenzelm
parents:
14798
diff
changeset
|
443 |
val root' = if root <> "prop" andalso is_logtype root then SynExt.logic else root; |
4703 | 444 |
val chars = Symbol.explode str; |
5692 | 445 |
val pts = Parser.parse gram root' (Lexicon.tokenize lexicon xids chars); |
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
446 |
val len = length pts; |
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
260
diff
changeset
|
447 |
|
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
448 |
val limit = ! ambiguity_limit; |
12292 | 449 |
fun show_pt pt = |
21772 | 450 |
Pretty.string_of (Ast.pretty_ast (hd (SynTrans.pts_to_asts ctxt (K NONE) [pt]))); |
260 | 451 |
in |
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
452 |
if len <= ! ambiguity_level then () |
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
453 |
else if ! ambiguity_is_error then error ("Ambiguous input " ^ quote str) |
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
454 |
else |
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
455 |
(warning (cat_lines |
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
456 |
(("Ambiguous input " ^ quote str ^ "\n" ^ |
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
457 |
"produces " ^ string_of_int len ^ " parse trees" ^ |
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
458 |
(if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":") :: |
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
459 |
map show_pt (Library.take (limit, pts))))); |
21772 | 460 |
SynTrans.pts_to_asts ctxt (lookup_tr parse_ast_trtab) pts |
260 | 461 |
end; |
462 |
||
463 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
464 |
(* read *) |
0 | 465 |
|
21772 | 466 |
fun read ctxt is_logtype (syn as Syntax (tabs, _)) ty str = |
0 | 467 |
let |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
468 |
val {parse_ruletab, parse_trtab, ...} = tabs; |
21772 | 469 |
val asts = read_asts ctxt is_logtype syn false (SynExt.typ_to_nonterm ty) str; |
0 | 470 |
in |
21772 | 471 |
SynTrans.asts_to_terms ctxt (lookup_tr parse_trtab) |
18931 | 472 |
(map (Ast.normalize_ast (Symtab.lookup_list parse_ruletab)) asts) |
0 | 473 |
end; |
474 |
||
475 |
||
22703 | 476 |
(* read terms *) |
477 |
||
24372
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
478 |
(*brute-force disambiguation via type-inference*) |
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
479 |
fun disambig _ _ [t] = t |
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
480 |
| disambig pp check ts = |
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
481 |
let |
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
482 |
val level = ! ambiguity_level; |
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
483 |
val limit = ! ambiguity_limit; |
24372
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
484 |
|
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
485 |
val ambiguity = length ts; |
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
486 |
fun ambig_msg () = |
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
487 |
if ambiguity > 1 andalso ambiguity <= level then |
24372
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
488 |
"Got more than one parse tree.\n\ |
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
489 |
\Retry with smaller Syntax.ambiguity_level for more information." |
24372
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
490 |
else ""; |
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
491 |
|
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
492 |
val errs = map check ts; |
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
493 |
val results = map_filter (fn (t, NONE) => SOME t | _ => NONE) (ts ~~ errs); |
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
494 |
val len = length results; |
24372
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
495 |
in |
25476
03da46cfab9e
standard_parse_term: check ambiguous results without changing the result yet;
wenzelm
parents:
25394
diff
changeset
|
496 |
if null results then cat_error (ambig_msg ()) (cat_lines (map_filter I errs)) |
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
497 |
else if len = 1 then |
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
498 |
(if ambiguity > level then |
24372
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
499 |
warning "Fortunately, only one parse tree is type correct.\n\ |
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
500 |
\You may still want to disambiguate your grammar or your input." |
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
501 |
else (); hd results) |
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
502 |
else cat_error (ambig_msg ()) (cat_lines |
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
503 |
(("Ambiguous input, " ^ string_of_int len ^ " terms are type correct" ^ |
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
504 |
(if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":") :: |
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
505 |
map (Pretty.string_of_term pp) (Library.take (limit, results)))) |
24372
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
506 |
end; |
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
507 |
|
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
508 |
fun standard_parse_term pp check get_sort map_const map_free map_type map_sort |
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
509 |
ctxt is_logtype syn ty str = |
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
510 |
read ctxt is_logtype syn ty str |
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
511 |
|> map (TypeExt.decode_term get_sort map_const map_free map_type map_sort) |
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
512 |
|> disambig (Printer.pp_show_brackets pp) check; |
22703 | 513 |
|
514 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
515 |
(* read types *) |
0 | 516 |
|
24278 | 517 |
fun standard_parse_typ ctxt syn get_sort map_sort str = |
21772 | 518 |
(case read ctxt (K false) syn SynExt.typeT str of |
22703 | 519 |
[t] => TypeExt.typ_of_term (get_sort (TypeExt.term_sorts map_sort t)) map_sort t |
8894 | 520 |
| _ => error "read_typ: ambiguous syntax"); |
144 | 521 |
|
522 |
||
8894 | 523 |
(* read sorts *) |
524 |
||
24278 | 525 |
fun standard_parse_sort ctxt syn map_sort str = |
21772 | 526 |
(case read ctxt (K false) syn TypeExt.sortT str of |
22703 | 527 |
[t] => TypeExt.sort_of_term map_sort t |
8894 | 528 |
| _ => error "read_sort: ambiguous syntax"); |
529 |
||
530 |
||
18 | 531 |
|
1158 | 532 |
(** prepare translation rules **) |
533 |
||
534 |
datatype 'a trrule = |
|
3526 | 535 |
ParseRule of 'a * 'a | |
536 |
PrintRule of 'a * 'a | |
|
537 |
ParsePrintRule of 'a * 'a; |
|
888
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
538 |
|
4618 | 539 |
fun map_trrule f (ParseRule (x, y)) = ParseRule (f x, f y) |
540 |
| map_trrule f (PrintRule (x, y)) = PrintRule (f x, f y) |
|
541 |
| map_trrule f (ParsePrintRule (x, y)) = ParsePrintRule (f x, f y); |
|
1158 | 542 |
|
15531 | 543 |
fun parse_rule (ParseRule pats) = SOME pats |
544 |
| parse_rule (PrintRule _) = NONE |
|
545 |
| parse_rule (ParsePrintRule pats) = SOME pats; |
|
1158 | 546 |
|
15531 | 547 |
fun print_rule (ParseRule _) = NONE |
548 |
| print_rule (PrintRule pats) = SOME (swap pats) |
|
549 |
| print_rule (ParsePrintRule pats) = SOME (swap pats); |
|
1158 | 550 |
|
551 |
||
19262 | 552 |
local |
553 |
||
1158 | 554 |
fun check_rule (rule as (lhs, rhs)) = |
5692 | 555 |
(case Ast.rule_error rule of |
15531 | 556 |
SOME msg => |
1158 | 557 |
error ("Error in syntax translation rule: " ^ msg ^ "\n" ^ |
5692 | 558 |
Ast.str_of_ast lhs ^ " -> " ^ Ast.str_of_ast rhs) |
15531 | 559 |
| NONE => rule); |
888
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
560 |
|
21772 | 561 |
fun read_pattern ctxt is_logtype syn (root, str) = |
18 | 562 |
let |
17079 | 563 |
val Syntax ({consts, ...}, _) = syn; |
18 | 564 |
|
5692 | 565 |
fun constify (ast as Ast.Constant _) = ast |
566 |
| constify (ast as Ast.Variable x) = |
|
20664 | 567 |
if member (op =) consts x orelse NameSpace.is_qualified x then Ast.Constant x |
3830 | 568 |
else ast |
5692 | 569 |
| constify (Ast.Appl asts) = Ast.Appl (map constify asts); |
18 | 570 |
in |
21772 | 571 |
(case read_asts ctxt is_logtype syn true root str of |
888
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
572 |
[ast] => constify ast |
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
573 |
| _ => error ("Syntactically ambiguous input: " ^ quote str)) |
18678 | 574 |
end handle ERROR msg => |
575 |
cat_error msg ("The error(s) above occurred in translation pattern " ^ |
|
888
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
576 |
quote str); |
0 | 577 |
|
1158 | 578 |
fun prep_rules rd_pat raw_rules = |
4618 | 579 |
let val rules = map (map_trrule rd_pat) raw_rules in |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19375
diff
changeset
|
580 |
(map check_rule (map_filter parse_rule rules), |
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19375
diff
changeset
|
581 |
map check_rule (map_filter print_rule rules)) |
1158 | 582 |
end |
18 | 583 |
|
19262 | 584 |
in |
585 |
||
586 |
val cert_rules = prep_rules I; |
|
587 |
||
21772 | 588 |
fun read_rules ctxt is_logtype syn = |
589 |
prep_rules (read_pattern ctxt is_logtype syn); |
|
19262 | 590 |
|
591 |
end; |
|
592 |
||
18 | 593 |
|
594 |
||
24923 | 595 |
(** unparse terms, typs, sorts **) |
18 | 596 |
|
24923 | 597 |
local |
598 |
||
599 |
fun unparse_t t_to_ast prt_t markup ctxt (syn as Syntax (tabs, _)) curried t = |
|
0 | 600 |
let |
2700 | 601 |
val {print_trtab, print_ruletab, print_ast_trtab, tokentrtab, prtabs, ...} = tabs; |
21772 | 602 |
val ast = t_to_ast ctxt (lookup_tr' print_trtab) t; |
0 | 603 |
in |
23631 | 604 |
Pretty.markup markup (prt_t ctxt curried prtabs (lookup_tr' print_ast_trtab) |
24613 | 605 |
(lookup_tokentr tokentrtab (print_mode_value ())) |
23631 | 606 |
(Ast.normalize_ast (Symtab.lookup_list print_ruletab) ast)) |
0 | 607 |
end; |
608 |
||
24923 | 609 |
in |
610 |
||
611 |
fun standard_unparse_term extern = |
|
612 |
unparse_t Printer.term_to_ast (Printer.pretty_term_ast extern) Markup.term; |
|
613 |
||
614 |
fun standard_unparse_typ ctxt syn = |
|
615 |
unparse_t Printer.typ_to_ast Printer.pretty_typ_ast Markup.typ ctxt syn false; |
|
616 |
||
617 |
fun standard_unparse_sort ctxt syn = |
|
618 |
unparse_t Printer.sort_to_ast Printer.pretty_typ_ast Markup.sort ctxt syn false; |
|
619 |
||
620 |
end; |
|
0 | 621 |
|
622 |
||
623 |
||
19262 | 624 |
(** modify syntax **) |
383
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents:
330
diff
changeset
|
625 |
|
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
626 |
fun ext_syntax f decls = update_syntax mode_default (f decls); |
383
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents:
330
diff
changeset
|
627 |
|
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
628 |
val update_type_gram = ext_syntax Mixfix.syn_ext_types; |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
629 |
val update_consts = ext_syntax SynExt.syn_ext_const_names; |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
630 |
val update_trfuns = ext_syntax SynExt.syn_ext_trfuns; |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
631 |
val update_advanced_trfuns = ext_syntax SynExt.syn_ext_advanced_trfuns; |
16613
76e57e08dcb5
proper treatment of advanced trfuns: pass thy argument;
wenzelm
parents:
15833
diff
changeset
|
632 |
val extend_tokentrfuns = ext_syntax SynExt.syn_ext_tokentrfuns; |
5692 | 633 |
|
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
634 |
fun update_const_gram is_logtype prmode decls = |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
635 |
update_syntax prmode (Mixfix.syn_ext_consts is_logtype decls); |
25387 | 636 |
|
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
637 |
fun remove_const_gram is_logtype prmode decls = |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
638 |
remove_syntax prmode (Mixfix.syn_ext_consts is_logtype decls); |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
639 |
|
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
640 |
fun update_trrules ctxt is_logtype syn = |
21772 | 641 |
ext_syntax SynExt.syn_ext_rules o read_rules ctxt is_logtype syn; |
19262 | 642 |
|
21772 | 643 |
fun remove_trrules ctxt is_logtype syn = |
24970 | 644 |
remove_syntax mode_default o SynExt.syn_ext_rules o read_rules ctxt is_logtype syn; |
19262 | 645 |
|
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
646 |
val update_trrules_i = ext_syntax SynExt.syn_ext_rules o cert_rules; |
24970 | 647 |
val remove_trrules_i = remove_syntax mode_default o SynExt.syn_ext_rules o cert_rules; |
5692 | 648 |
|
649 |
||
24263 | 650 |
|
24768 | 651 |
(** inner syntax operations **) |
24263 | 652 |
|
24768 | 653 |
(* (un)parsing *) |
24263 | 654 |
|
655 |
local |
|
24768 | 656 |
type operations = |
657 |
{parse_sort: Proof.context -> string -> sort, |
|
658 |
parse_typ: Proof.context -> string -> typ, |
|
659 |
parse_term: Proof.context -> string -> term, |
|
660 |
parse_prop: Proof.context -> string -> term, |
|
661 |
unparse_sort: Proof.context -> sort -> Pretty.T, |
|
662 |
unparse_typ: Proof.context -> typ -> Pretty.T, |
|
663 |
unparse_term: Proof.context -> term -> Pretty.T}; |
|
664 |
||
665 |
val operations = ref (NONE: operations option); |
|
666 |
||
667 |
fun operation which ctxt x = |
|
668 |
(case ! operations of |
|
669 |
NONE => error "Inner syntax operations not yet installed" |
|
670 |
| SOME ops => which ops ctxt x); |
|
24263 | 671 |
in |
672 |
||
24768 | 673 |
val parse_sort = operation #parse_sort; |
674 |
val parse_typ = operation #parse_typ; |
|
675 |
val parse_term = operation #parse_term; |
|
676 |
val parse_prop = operation #parse_prop; |
|
677 |
val unparse_sort = operation #unparse_sort; |
|
678 |
val unparse_typ = operation #unparse_typ; |
|
679 |
val unparse_term = operation #unparse_term; |
|
680 |
||
681 |
fun install_operations ops = CRITICAL (fn () => |
|
682 |
if is_some (! operations) then error "Inner syntax operations already installed" |
|
683 |
else operations := SOME ops); |
|
684 |
||
24263 | 685 |
end; |
686 |
||
687 |
||
24768 | 688 |
(* context-sensitive (un)checking *) |
24263 | 689 |
|
24488 | 690 |
local |
691 |
||
24768 | 692 |
type key = int * bool; |
25060 | 693 |
type 'a check = 'a list -> Proof.context -> ('a list * Proof.context) option; |
24768 | 694 |
|
24488 | 695 |
structure Checks = GenericDataFun |
24263 | 696 |
( |
24488 | 697 |
type T = |
24768 | 698 |
((key * ((string * typ check) * stamp) list) list * |
699 |
(key * ((string * term check) * stamp) list) list); |
|
24488 | 700 |
val empty = ([], []); |
24263 | 701 |
val extend = I; |
24488 | 702 |
fun merge _ ((typ_checks1, term_checks1), (typ_checks2, term_checks2)) : T = |
24768 | 703 |
(AList.join (op =) (K (Library.merge (eq_snd (op =)))) (typ_checks1, typ_checks2), |
704 |
AList.join (op =) (K (Library.merge (eq_snd (op =)))) (term_checks1, term_checks2)); |
|
24263 | 705 |
); |
706 |
||
24768 | 707 |
fun gen_add which (key: key) name f = |
708 |
Checks.map (which (AList.map_default op = (key, []) (cons ((name, f), stamp ())))); |
|
709 |
||
25060 | 710 |
fun check_stage fs = perhaps_loop (perhaps_apply (map uncurry fs)); |
711 |
||
712 |
fun gen_check which uncheck ctxt0 xs0 = |
|
24263 | 713 |
let |
24768 | 714 |
val funs = which (Checks.get (Context.Proof ctxt0)) |
715 |
|> map_filter (fn ((i, u), fs) => if uncheck = u then SOME (i, map (snd o fst) fs) else NONE) |
|
25060 | 716 |
|> Library.sort (int_ord o pairself fst) |> map snd |
717 |
|> not uncheck ? map rev; |
|
718 |
val check_all = perhaps_apply (map check_stage funs); |
|
719 |
in #1 (perhaps check_all (xs0, ctxt0)) end; |
|
24768 | 720 |
|
721 |
fun map_sort f S = |
|
722 |
(case f (TFree ("", S)) of |
|
723 |
TFree ("", S') => S' |
|
24923 | 724 |
| _ => raise TYPE ("map_sort", [TFree ("", S)], [])); |
24488 | 725 |
|
726 |
in |
|
24263 | 727 |
|
24768 | 728 |
fun print_checks ctxt = |
729 |
let |
|
730 |
fun split_checks checks = |
|
731 |
List.partition (fn ((_, un), _) => not un) checks |
|
732 |
|> pairself (map (fn ((i, _), fs) => (i, map (fst o fst) fs)) |
|
733 |
#> sort (int_ord o pairself fst)); |
|
734 |
fun pretty_checks kind checks = |
|
735 |
checks |> map (fn (i, names) => Pretty.block |
|
736 |
[Pretty.str (kind ^ " (stage " ^ signed_string_of_int i ^ "):"), |
|
737 |
Pretty.brk 1, Pretty.strs names]); |
|
738 |
||
739 |
val (typs, terms) = Checks.get (Context.Proof ctxt); |
|
740 |
val (typ_checks, typ_unchecks) = split_checks typs; |
|
741 |
val (term_checks, term_unchecks) = split_checks terms; |
|
742 |
in |
|
743 |
pretty_checks "typ_checks" typ_checks @ |
|
744 |
pretty_checks "term_checks" term_checks @ |
|
745 |
pretty_checks "typ_unchecks" typ_unchecks @ |
|
746 |
pretty_checks "term_unchecks" term_unchecks |
|
747 |
end |> Pretty.chunks |> Pretty.writeln; |
|
748 |
||
749 |
fun add_typ_check stage = gen_add apfst (stage, false); |
|
750 |
fun add_term_check stage = gen_add apsnd (stage, false); |
|
751 |
fun add_typ_uncheck stage = gen_add apfst (stage, true); |
|
752 |
fun add_term_uncheck stage = gen_add apsnd (stage, true); |
|
753 |
||
25060 | 754 |
val check_typs = gen_check fst false; |
755 |
val check_terms = gen_check snd false; |
|
24680 | 756 |
fun check_props ctxt = map (TypeInfer.constrain propT) #> check_terms ctxt; |
24488 | 757 |
|
24512 | 758 |
val check_typ = singleton o check_typs; |
759 |
val check_term = singleton o check_terms; |
|
760 |
val check_prop = singleton o check_props; |
|
24768 | 761 |
val check_sort = map_sort o check_typ; |
24263 | 762 |
|
25060 | 763 |
val uncheck_typs = gen_check fst true; |
764 |
val uncheck_terms = gen_check snd true; |
|
24768 | 765 |
val uncheck_sort = map_sort o singleton o uncheck_typs; |
24488 | 766 |
|
767 |
end; |
|
768 |
||
24263 | 769 |
|
24923 | 770 |
(* derived operations for classrel and arity *) |
771 |
||
772 |
val uncheck_classrel = map o singleton o uncheck_sort; |
|
773 |
||
774 |
fun unparse_classrel ctxt cs = Pretty.block (flat |
|
775 |
(separate [Pretty.str " <", Pretty.brk 1] (map (single o unparse_sort ctxt o single) cs))); |
|
776 |
||
777 |
fun uncheck_arity ctxt (a, Ss, S) = |
|
778 |
let |
|
779 |
val T = Type (a, replicate (length Ss) dummyT); |
|
780 |
val a' = |
|
781 |
(case singleton (uncheck_typs ctxt) T of |
|
782 |
Type (a', _) => a' |
|
783 |
| T => raise TYPE ("uncheck_arity", [T], [])); |
|
784 |
val Ss' = map (uncheck_sort ctxt) Ss; |
|
785 |
val S' = uncheck_sort ctxt S; |
|
786 |
in (a', Ss', S') end; |
|
787 |
||
788 |
fun unparse_arity ctxt (a, Ss, S) = |
|
789 |
let |
|
25043 | 790 |
val prtT = unparse_typ ctxt (Type (a, [])); |
24923 | 791 |
val dom = |
792 |
if null Ss then [] |
|
793 |
else [Pretty.list "(" ")" (map (unparse_sort ctxt) Ss), Pretty.brk 1]; |
|
25043 | 794 |
in Pretty.block ([prtT, Pretty.str " ::", Pretty.brk 1] @ dom @ [unparse_sort ctxt S]) end; |
24923 | 795 |
|
796 |
||
797 |
(* read = parse + check *) |
|
24263 | 798 |
|
799 |
fun read_sort ctxt = parse_sort ctxt #> check_sort ctxt; |
|
800 |
fun read_typ ctxt = parse_typ ctxt #> singleton (check_typs ctxt); |
|
801 |
||
802 |
fun read_terms ctxt = map (parse_term ctxt) #> check_terms ctxt; |
|
803 |
fun read_props ctxt = map (parse_prop ctxt) #> check_props ctxt; |
|
804 |
||
805 |
val read_term = singleton o read_terms; |
|
806 |
val read_prop = singleton o read_props; |
|
807 |
||
24709 | 808 |
val read_sort_global = read_sort o ProofContext.init; |
809 |
val read_typ_global = read_typ o ProofContext.init; |
|
810 |
val read_term_global = read_term o ProofContext.init; |
|
811 |
val read_prop_global = read_prop o ProofContext.init; |
|
24278 | 812 |
|
24263 | 813 |
|
24923 | 814 |
(* pretty = uncheck + unparse *) |
815 |
||
816 |
fun pretty_sort ctxt = uncheck_sort ctxt #> unparse_sort ctxt; |
|
817 |
fun pretty_classrel ctxt = uncheck_classrel ctxt #> unparse_classrel ctxt; |
|
818 |
fun pretty_arity ctxt = uncheck_arity ctxt #> unparse_arity ctxt; |
|
819 |
fun pretty_typ ctxt = singleton (uncheck_typs ctxt) #> unparse_typ ctxt; |
|
820 |
fun pretty_term ctxt = singleton (uncheck_terms ctxt) #> unparse_term ctxt; |
|
821 |
||
822 |
val string_of_sort = Pretty.string_of oo pretty_sort; |
|
823 |
val string_of_classrel = Pretty.string_of oo pretty_classrel; |
|
824 |
val string_of_arity = Pretty.string_of oo pretty_arity; |
|
825 |
val string_of_typ = Pretty.string_of oo pretty_typ; |
|
826 |
val string_of_term = Pretty.string_of oo pretty_term; |
|
827 |
||
24970 | 828 |
(*pp operations -- deferred evaluation*) |
829 |
fun pp ctxt = Pretty.pp |
|
830 |
(fn x => pretty_term ctxt x, |
|
831 |
fn x => pretty_typ ctxt x, |
|
832 |
fn x => pretty_sort ctxt x, |
|
833 |
fn x => pretty_classrel ctxt x, |
|
834 |
fn x => pretty_arity ctxt x); |
|
24923 | 835 |
|
15833
78109c7012ed
removed token_trans.ML (some content moved to syn_ext.ML);
wenzelm
parents:
15759
diff
changeset
|
836 |
(*export parts of internal Syntax structures*) |
78109c7012ed
removed token_trans.ML (some content moved to syn_ext.ML);
wenzelm
parents:
15759
diff
changeset
|
837 |
open Lexicon SynExt Ast Parser TypeExt SynTrans Mixfix Printer; |
2366 | 838 |
|
0 | 839 |
end; |
5692 | 840 |
|
841 |
structure BasicSyntax: BASIC_SYNTAX = Syntax; |
|
842 |
open BasicSyntax; |
|
23923 | 843 |
|
844 |
structure Hidden = struct end; |
|
845 |
structure Ast = Hidden; |
|
846 |
structure SynExt = Hidden; |
|
847 |
structure Parser = Hidden; |
|
848 |
structure TypeExt = Hidden; |
|
849 |
structure SynTrans = Hidden; |
|
850 |
structure Mixfix = Hidden; |
|
851 |
structure Printer = Hidden; |