author | wenzelm |
Mon, 18 Apr 2011 11:13:29 +0200 | |
changeset 42383 | 0ae4ad40d7b5 |
parent 42382 | dcd983ee2c29 |
child 42402 | c7139609b67d |
permissions | -rw-r--r-- |
18 | 1 |
(* Title: Pure/Syntax/syntax.ML |
0 | 2 |
Author: Tobias Nipkow and Markus Wenzel, TU Muenchen |
18 | 3 |
|
24263 | 4 |
Standard Isabelle syntax, based on arbitrary context-free grammars |
5 |
(specified by mixfix declarations). |
|
0 | 6 |
*) |
7 |
||
8 |
signature SYNTAX = |
|
2383 | 9 |
sig |
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42289
diff
changeset
|
10 |
val const: string -> term |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42289
diff
changeset
|
11 |
val free: string -> term |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42289
diff
changeset
|
12 |
val var: indexname -> term |
42382
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
13 |
type operations |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
14 |
val install_operations: operations -> unit |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
15 |
val root: string Config.T |
42268 | 16 |
val positions_raw: Config.raw |
17 |
val positions: bool Config.T |
|
18 |
val ambiguity_enabled: bool Config.T |
|
19 |
val ambiguity_level_raw: Config.raw |
|
20 |
val ambiguity_level: int Config.T |
|
21 |
val ambiguity_limit: int Config.T |
|
30573 | 22 |
val read_token: string -> Symbol_Pos.T list * Position.T |
38238 | 23 |
val parse_token: Proof.context -> Markup.T -> string -> Symbol_Pos.T list * Position.T |
24263 | 24 |
val parse_sort: Proof.context -> string -> sort |
25 |
val parse_typ: Proof.context -> string -> typ |
|
26 |
val parse_term: Proof.context -> string -> term |
|
27 |
val parse_prop: Proof.context -> string -> term |
|
24768 | 28 |
val unparse_sort: Proof.context -> sort -> Pretty.T |
24923 | 29 |
val unparse_classrel: Proof.context -> class list -> Pretty.T |
30 |
val unparse_arity: Proof.context -> arity -> Pretty.T |
|
24768 | 31 |
val unparse_typ: Proof.context -> typ -> Pretty.T |
32 |
val unparse_term: Proof.context -> term -> Pretty.T |
|
33 |
val print_checks: Proof.context -> unit |
|
34 |
val add_typ_check: int -> string -> |
|
25060 | 35 |
(typ list -> Proof.context -> (typ list * Proof.context) option) -> |
24768 | 36 |
Context.generic -> Context.generic |
37 |
val add_term_check: int -> string -> |
|
25060 | 38 |
(term list -> Proof.context -> (term list * Proof.context) option) -> |
24768 | 39 |
Context.generic -> Context.generic |
40 |
val add_typ_uncheck: int -> string -> |
|
25060 | 41 |
(typ list -> Proof.context -> (typ list * Proof.context) option) -> |
24768 | 42 |
Context.generic -> Context.generic |
43 |
val add_term_uncheck: int -> string -> |
|
25060 | 44 |
(term list -> Proof.context -> (term list * Proof.context) option) -> |
24768 | 45 |
Context.generic -> Context.generic |
42382
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
46 |
val typ_check: Proof.context -> typ list -> typ list |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
47 |
val term_check: Proof.context -> term list -> term list |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
48 |
val typ_uncheck: Proof.context -> typ list -> typ list |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
49 |
val term_uncheck: Proof.context -> term list -> term list |
24488 | 50 |
val check_sort: Proof.context -> sort -> sort |
24512 | 51 |
val check_typ: Proof.context -> typ -> typ |
52 |
val check_term: Proof.context -> term -> term |
|
53 |
val check_prop: Proof.context -> term -> term |
|
24488 | 54 |
val check_typs: Proof.context -> typ list -> typ list |
24263 | 55 |
val check_terms: Proof.context -> term list -> term list |
56 |
val check_props: Proof.context -> term list -> term list |
|
24923 | 57 |
val uncheck_sort: Proof.context -> sort -> sort |
58 |
val uncheck_arity: Proof.context -> arity -> arity |
|
59 |
val uncheck_classrel: Proof.context -> class list -> class list |
|
24768 | 60 |
val uncheck_typs: Proof.context -> typ list -> typ list |
61 |
val uncheck_terms: Proof.context -> term list -> term list |
|
24263 | 62 |
val read_sort: Proof.context -> string -> sort |
63 |
val read_typ: Proof.context -> string -> typ |
|
24488 | 64 |
val read_term: Proof.context -> string -> term |
65 |
val read_prop: Proof.context -> string -> term |
|
24263 | 66 |
val read_terms: Proof.context -> string list -> term list |
67 |
val read_props: Proof.context -> string list -> term list |
|
24709 | 68 |
val read_sort_global: theory -> string -> sort |
69 |
val read_typ_global: theory -> string -> typ |
|
70 |
val read_term_global: theory -> string -> term |
|
71 |
val read_prop_global: theory -> string -> term |
|
24923 | 72 |
val pretty_term: Proof.context -> term -> Pretty.T |
73 |
val pretty_typ: Proof.context -> typ -> Pretty.T |
|
74 |
val pretty_sort: Proof.context -> sort -> Pretty.T |
|
75 |
val pretty_classrel: Proof.context -> class list -> Pretty.T |
|
76 |
val pretty_arity: Proof.context -> arity -> Pretty.T |
|
77 |
val string_of_term: Proof.context -> term -> string |
|
78 |
val string_of_typ: Proof.context -> typ -> string |
|
79 |
val string_of_sort: Proof.context -> sort -> string |
|
80 |
val string_of_classrel: Proof.context -> class list -> string |
|
81 |
val string_of_arity: Proof.context -> arity -> string |
|
26951
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
82 |
val is_pretty_global: Proof.context -> bool |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
83 |
val set_pretty_global: bool -> Proof.context -> Proof.context |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
84 |
val init_pretty_global: theory -> Proof.context |
42383
0ae4ad40d7b5
simplified pretty printing context, which is only required for certain kernel operations;
wenzelm
parents:
42382
diff
changeset
|
85 |
val init_pretty: Context.pretty -> Proof.context |
26951
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
86 |
val pretty_term_global: theory -> term -> Pretty.T |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
87 |
val pretty_typ_global: theory -> typ -> Pretty.T |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
88 |
val pretty_sort_global: theory -> sort -> Pretty.T |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
89 |
val string_of_term_global: theory -> term -> string |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
90 |
val string_of_typ_global: theory -> typ -> string |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
91 |
val string_of_sort_global: theory -> sort -> string |
39135
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
92 |
type syntax |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
93 |
val eq_syntax: syntax * syntax -> bool |
42298
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
94 |
val lookup_const: syntax -> string -> string option |
39135
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
95 |
val is_keyword: syntax -> string -> bool |
42251 | 96 |
val tokenize: syntax -> bool -> Symbol_Pos.T list -> Lexicon.token list |
97 |
val parse: Proof.context -> syntax -> string -> Lexicon.token list -> Parser.parsetree list |
|
42253 | 98 |
val parse_ast_translation: syntax -> string -> (Proof.context -> Ast.ast list -> Ast.ast) option |
42255 | 99 |
val parse_rules: syntax -> string -> (Ast.ast * Ast.ast) list |
42253 | 100 |
val parse_translation: syntax -> string -> (Proof.context -> term list -> term) option |
42254 | 101 |
val print_translation: syntax -> string -> |
102 |
Proof.context -> typ -> term list -> term (*exception Match*) |
|
42255 | 103 |
val print_rules: syntax -> string -> (Ast.ast * Ast.ast) list |
42254 | 104 |
val print_ast_translation: syntax -> string -> |
105 |
Proof.context -> Ast.ast list -> Ast.ast (*exception Match*) |
|
42255 | 106 |
val prtabs: syntax -> Printer.prtabs |
39135
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
107 |
type mode |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
108 |
val mode_default: mode |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
109 |
val mode_input: mode |
42294 | 110 |
val empty_syntax: syntax |
39135
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
111 |
val merge_syntaxes: syntax -> syntax -> syntax |
42294 | 112 |
val token_markers: string list |
39135
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
113 |
val basic_nonterms: string list |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
114 |
val print_gram: syntax -> unit |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
115 |
val print_trans: syntax -> unit |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
116 |
val print_syntax: syntax -> unit |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
117 |
val guess_infix: syntax -> string -> mixfix option |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
118 |
datatype 'a trrule = |
42204 | 119 |
Parse_Rule of 'a * 'a | |
120 |
Print_Rule of 'a * 'a | |
|
121 |
Parse_Print_Rule of 'a * 'a |
|
39135
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
122 |
val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
123 |
val update_trfuns: |
42224
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents:
42223
diff
changeset
|
124 |
(string * ((Ast.ast list -> Ast.ast) * stamp)) list * |
39135
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
125 |
(string * ((term list -> term) * stamp)) list * |
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
126 |
(string * ((typ -> term list -> term) * stamp)) list * |
42224
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents:
42223
diff
changeset
|
127 |
(string * ((Ast.ast list -> Ast.ast) * stamp)) list -> syntax -> syntax |
39135
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
128 |
val update_advanced_trfuns: |
42224
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents:
42223
diff
changeset
|
129 |
(string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list * |
39135
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
130 |
(string * ((Proof.context -> term list -> term) * stamp)) list * |
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
131 |
(string * ((Proof.context -> typ -> term list -> term) * stamp)) list * |
42224
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents:
42223
diff
changeset
|
132 |
(string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list -> syntax -> syntax |
39135
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
133 |
val update_type_gram: bool -> mode -> (string * typ * mixfix) list -> syntax -> syntax |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
134 |
val update_const_gram: bool -> (string -> bool) -> |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
135 |
mode -> (string * typ * mixfix) list -> syntax -> syntax |
42224
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents:
42223
diff
changeset
|
136 |
val update_trrules: Ast.ast trrule list -> syntax -> syntax |
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents:
42223
diff
changeset
|
137 |
val remove_trrules: Ast.ast trrule list -> syntax -> syntax |
2383 | 138 |
end; |
0 | 139 |
|
12094 | 140 |
structure Syntax: SYNTAX = |
0 | 141 |
struct |
142 |
||
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42289
diff
changeset
|
143 |
val const = Lexicon.const; |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42289
diff
changeset
|
144 |
val free = Lexicon.free; |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42289
diff
changeset
|
145 |
val var = Lexicon.var; |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42289
diff
changeset
|
146 |
|
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42289
diff
changeset
|
147 |
|
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
148 |
|
39135
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
149 |
(** inner syntax operations **) |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
150 |
|
42382
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
151 |
(* back-patched operations *) |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
152 |
|
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
153 |
type operations = |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
154 |
{parse_sort: Proof.context -> string -> sort, |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
155 |
parse_typ: Proof.context -> string -> typ, |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
156 |
parse_term: Proof.context -> string -> term, |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
157 |
parse_prop: Proof.context -> string -> term, |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
158 |
unparse_sort: Proof.context -> sort -> Pretty.T, |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
159 |
unparse_typ: Proof.context -> typ -> Pretty.T, |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
160 |
unparse_term: Proof.context -> term -> Pretty.T, |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
161 |
check_typs: Proof.context -> typ list -> typ list, |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
162 |
check_terms: Proof.context -> term list -> term list, |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
163 |
check_props: Proof.context -> term list -> term list, |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
164 |
uncheck_typs: Proof.context -> typ list -> typ list, |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
165 |
uncheck_terms: Proof.context -> term list -> term list}; |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
166 |
|
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
167 |
val operations: operations Single_Assignment.var = Single_Assignment.var "Syntax.operations"; |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
168 |
fun install_operations ops = Single_Assignment.assign operations ops; |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
169 |
|
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
170 |
fun operation which ctxt x = |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
171 |
(case Single_Assignment.peek operations of |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
172 |
NONE => raise Fail "Inner syntax operations not installed" |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
173 |
| SOME ops => which ops ctxt x); |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
174 |
|
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
175 |
|
42268 | 176 |
(* configuration options *) |
177 |
||
42293
6cca0343ea48
renamed sprop "prop#" to "prop'" -- proper identifier;
wenzelm
parents:
42290
diff
changeset
|
178 |
val root = Config.string (Config.declare "syntax_root" (K (Config.String "any"))); |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
179 |
|
42268 | 180 |
val positions_raw = Config.declare "syntax_positions" (fn _ => Config.Bool true); |
181 |
val positions = Config.bool positions_raw; |
|
182 |
||
183 |
val ambiguity_enabled = |
|
184 |
Config.bool (Config.declare "syntax_ambiguity_enabled" (fn _ => Config.Bool true)); |
|
185 |
||
186 |
val ambiguity_level_raw = Config.declare "syntax_ambiguity_level" (fn _ => Config.Int 1); |
|
187 |
val ambiguity_level = Config.int ambiguity_level_raw; |
|
188 |
||
189 |
val ambiguity_limit = |
|
190 |
Config.int (Config.declare "syntax_ambiguity_limit" (fn _ => Config.Int 10)); |
|
191 |
||
192 |
||
39135
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
193 |
(* read token -- with optional YXML encoding of position *) |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
194 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
195 |
fun read_token str = |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
196 |
let |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
197 |
val tree = YXML.parse str handle Fail msg => error msg; |
39555
ccb223a4d49c
added XML.content_of convenience -- cover XML.body, which is the general situation;
wenzelm
parents:
39510
diff
changeset
|
198 |
val text = XML.content_of [tree]; |
39135
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
199 |
val pos = |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
200 |
(case tree of |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
201 |
XML.Elem ((name, props), _) => |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
202 |
if name = Markup.tokenN then Position.of_properties props |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
203 |
else Position.none |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
204 |
| XML.Text _ => Position.none); |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
205 |
in (Symbol_Pos.explode (text, pos), pos) end; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
206 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
207 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
208 |
(* (un)parsing *) |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
209 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
210 |
fun parse_token ctxt markup str = |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
211 |
let |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
212 |
val (syms, pos) = read_token str; |
39507
839873937ddd
tuned signature of (Context_)Position.report variants;
wenzelm
parents:
39288
diff
changeset
|
213 |
val _ = Context_Position.report ctxt pos markup; |
39135
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
214 |
in (syms, pos) end; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
215 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
216 |
val parse_sort = operation #parse_sort; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
217 |
val parse_typ = operation #parse_typ; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
218 |
val parse_term = operation #parse_term; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
219 |
val parse_prop = operation #parse_prop; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
220 |
val unparse_sort = operation #unparse_sort; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
221 |
val unparse_typ = operation #unparse_typ; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
222 |
val unparse_term = operation #unparse_term; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
223 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
224 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
225 |
(* context-sensitive (un)checking *) |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
226 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
227 |
local |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
228 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
229 |
type key = int * bool; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
230 |
type 'a check = 'a list -> Proof.context -> ('a list * Proof.context) option; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
231 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
232 |
structure Checks = Generic_Data |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
233 |
( |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
234 |
type T = |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
235 |
((key * ((string * typ check) * stamp) list) list * |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
236 |
(key * ((string * term check) * stamp) list) list); |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
237 |
val empty = ([], []); |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
238 |
val extend = I; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
239 |
fun merge ((typ_checks1, term_checks1), (typ_checks2, term_checks2)) : T = |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
240 |
(AList.join (op =) (K (Library.merge (eq_snd (op =)))) (typ_checks1, typ_checks2), |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
241 |
AList.join (op =) (K (Library.merge (eq_snd (op =)))) (term_checks1, term_checks2)); |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
242 |
); |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
243 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
244 |
fun gen_add which (key: key) name f = |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
245 |
Checks.map (which (AList.map_default op = (key, []) (cons ((name, f), stamp ())))); |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
246 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
247 |
fun check_stage fs = perhaps_loop (perhaps_apply (map uncurry fs)); |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
248 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
249 |
fun gen_check which uncheck ctxt0 xs0 = |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
250 |
let |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
251 |
val funs = which (Checks.get (Context.Proof ctxt0)) |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
252 |
|> map_filter (fn ((i, u), fs) => if uncheck = u then SOME (i, map (snd o fst) fs) else NONE) |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
253 |
|> Library.sort (int_ord o pairself fst) |> map snd |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
254 |
|> not uncheck ? map rev; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
255 |
val check_all = perhaps_apply (map check_stage funs); |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
256 |
in #1 (perhaps check_all (xs0, ctxt0)) end; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
257 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
258 |
fun map_sort f S = |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
259 |
(case f (TFree ("", S)) of |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
260 |
TFree ("", S') => S' |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
261 |
| _ => raise TYPE ("map_sort", [TFree ("", S)], [])); |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
262 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
263 |
in |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
264 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
265 |
fun print_checks ctxt = |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
266 |
let |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
267 |
fun split_checks checks = |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
268 |
List.partition (fn ((_, un), _) => not un) checks |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
269 |
|> pairself (map (fn ((i, _), fs) => (i, map (fst o fst) fs)) |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
270 |
#> sort (int_ord o pairself fst)); |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
271 |
fun pretty_checks kind checks = |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
272 |
checks |> map (fn (i, names) => Pretty.block |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
273 |
[Pretty.str (kind ^ " (stage " ^ signed_string_of_int i ^ "):"), |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
274 |
Pretty.brk 1, Pretty.strs names]); |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
275 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
276 |
val (typs, terms) = Checks.get (Context.Proof ctxt); |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
277 |
val (typ_checks, typ_unchecks) = split_checks typs; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
278 |
val (term_checks, term_unchecks) = split_checks terms; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
279 |
in |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
280 |
pretty_checks "typ_checks" typ_checks @ |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
281 |
pretty_checks "term_checks" term_checks @ |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
282 |
pretty_checks "typ_unchecks" typ_unchecks @ |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
283 |
pretty_checks "term_unchecks" term_unchecks |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
284 |
end |> Pretty.chunks |> Pretty.writeln; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
285 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
286 |
fun add_typ_check stage = gen_add apfst (stage, false); |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
287 |
fun add_term_check stage = gen_add apsnd (stage, false); |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
288 |
fun add_typ_uncheck stage = gen_add apfst (stage, true); |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
289 |
fun add_term_uncheck stage = gen_add apsnd (stage, true); |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
290 |
|
42382
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
291 |
val typ_check = gen_check fst false; |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
292 |
val term_check = gen_check snd false; |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
293 |
val typ_uncheck = gen_check fst true; |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
294 |
val term_uncheck = gen_check snd true; |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
295 |
|
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
296 |
val check_typs = operation #check_typs; |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
297 |
val check_terms = operation #check_terms; |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
298 |
val check_props = operation #check_props; |
39135
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
299 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
300 |
val check_typ = singleton o check_typs; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
301 |
val check_term = singleton o check_terms; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
302 |
val check_prop = singleton o check_props; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
303 |
val check_sort = map_sort o check_typ; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
304 |
|
42382
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
305 |
val uncheck_typs = operation #uncheck_typs; |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42360
diff
changeset
|
306 |
val uncheck_terms = operation #uncheck_terms; |
39135
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
307 |
val uncheck_sort = map_sort o singleton o uncheck_typs; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
308 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
309 |
end; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
310 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
311 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
312 |
(* derived operations for classrel and arity *) |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
313 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
314 |
val uncheck_classrel = map o singleton o uncheck_sort; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
315 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
316 |
fun unparse_classrel ctxt cs = Pretty.block (flat |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
317 |
(separate [Pretty.str " <", Pretty.brk 1] (map (single o unparse_sort ctxt o single) cs))); |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
318 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
319 |
fun uncheck_arity ctxt (a, Ss, S) = |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
320 |
let |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
321 |
val T = Type (a, replicate (length Ss) dummyT); |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
322 |
val a' = |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
323 |
(case singleton (uncheck_typs ctxt) T of |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
324 |
Type (a', _) => a' |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
325 |
| T => raise TYPE ("uncheck_arity", [T], [])); |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
326 |
val Ss' = map (uncheck_sort ctxt) Ss; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
327 |
val S' = uncheck_sort ctxt S; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
328 |
in (a', Ss', S') end; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
329 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
330 |
fun unparse_arity ctxt (a, Ss, S) = |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
331 |
let |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
332 |
val prtT = unparse_typ ctxt (Type (a, [])); |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
333 |
val dom = |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
334 |
if null Ss then [] |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
335 |
else [Pretty.list "(" ")" (map (unparse_sort ctxt) Ss), Pretty.brk 1]; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
336 |
in Pretty.block ([prtT, Pretty.str " ::", Pretty.brk 1] @ dom @ [unparse_sort ctxt S]) end; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
337 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
338 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
339 |
(* read = parse + check *) |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
340 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
341 |
fun read_sort ctxt = parse_sort ctxt #> check_sort ctxt; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
342 |
fun read_typ ctxt = parse_typ ctxt #> singleton (check_typs ctxt); |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
343 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
344 |
fun read_terms ctxt = map (parse_term ctxt) #> check_terms ctxt; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
345 |
fun read_props ctxt = map (parse_prop ctxt) #> check_props ctxt; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
346 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
347 |
val read_term = singleton o read_terms; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
348 |
val read_prop = singleton o read_props; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
349 |
|
42360 | 350 |
val read_sort_global = read_sort o Proof_Context.init_global; |
351 |
val read_typ_global = read_typ o Proof_Context.init_global; |
|
352 |
val read_term_global = read_term o Proof_Context.init_global; |
|
353 |
val read_prop_global = read_prop o Proof_Context.init_global; |
|
39135
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
354 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
355 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
356 |
(* pretty = uncheck + unparse *) |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
357 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
358 |
fun pretty_term ctxt = singleton (uncheck_terms ctxt) #> unparse_term ctxt; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
359 |
fun pretty_typ ctxt = singleton (uncheck_typs ctxt) #> unparse_typ ctxt; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
360 |
fun pretty_sort ctxt = uncheck_sort ctxt #> unparse_sort ctxt; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
361 |
fun pretty_classrel ctxt = uncheck_classrel ctxt #> unparse_classrel ctxt; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
362 |
fun pretty_arity ctxt = uncheck_arity ctxt #> unparse_arity ctxt; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
363 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
364 |
val string_of_term = Pretty.string_of oo pretty_term; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
365 |
val string_of_typ = Pretty.string_of oo pretty_typ; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
366 |
val string_of_sort = Pretty.string_of oo pretty_sort; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
367 |
val string_of_classrel = Pretty.string_of oo pretty_classrel; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
368 |
val string_of_arity = Pretty.string_of oo pretty_arity; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
369 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
370 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
371 |
(* global pretty printing *) |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
372 |
|
39508
dfacdb01e1ec
simplified some internal flags using Config.T instead of full-blown Proof_Data;
wenzelm
parents:
39507
diff
changeset
|
373 |
val pretty_global = Config.bool (Config.declare "Syntax.pretty_global" (K (Config.Bool false))); |
dfacdb01e1ec
simplified some internal flags using Config.T instead of full-blown Proof_Data;
wenzelm
parents:
39507
diff
changeset
|
374 |
fun is_pretty_global ctxt = Config.get ctxt pretty_global; |
dfacdb01e1ec
simplified some internal flags using Config.T instead of full-blown Proof_Data;
wenzelm
parents:
39507
diff
changeset
|
375 |
val set_pretty_global = Config.put pretty_global; |
42360 | 376 |
val init_pretty_global = set_pretty_global true o Proof_Context.init_global; |
42383
0ae4ad40d7b5
simplified pretty printing context, which is only required for certain kernel operations;
wenzelm
parents:
42382
diff
changeset
|
377 |
val init_pretty = Context.pretty_context init_pretty_global; |
39135
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
378 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
379 |
val pretty_term_global = pretty_term o init_pretty_global; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
380 |
val pretty_typ_global = pretty_typ o init_pretty_global; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
381 |
val pretty_sort_global = pretty_sort o init_pretty_global; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
382 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
383 |
val string_of_term_global = string_of_term o init_pretty_global; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
384 |
val string_of_typ_global = string_of_typ o init_pretty_global; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
385 |
val string_of_sort_global = string_of_sort o init_pretty_global; |
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
386 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
387 |
|
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents:
39126
diff
changeset
|
388 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
389 |
(** tables of translation functions **) |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
390 |
|
5692 | 391 |
(* parse (ast) translations *) |
392 |
||
23655
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23631
diff
changeset
|
393 |
fun err_dup_trfun name c = |
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23631
diff
changeset
|
394 |
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
|
395 |
|
42253 | 396 |
fun lookup_tr tab c = Option.map fst (Symtab.lookup tab c); |
397 |
||
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
398 |
fun remove_trtab trfuns = fold (Symtab.remove Syntax_Ext.eq_trfun) trfuns; |
21536
f119c730f509
extend_trtab: allow identical trfuns to be overwritten;
wenzelm
parents:
20784
diff
changeset
|
399 |
|
29004 | 400 |
fun update_trtab name trfuns tab = fold Symtab.update_new trfuns (remove_trtab trfuns tab) |
23655
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23631
diff
changeset
|
401 |
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
|
402 |
|
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
403 |
fun merge_trtabs name tab1 tab2 = Symtab.merge Syntax_Ext.eq_trfun (tab1, tab2) |
23655
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23631
diff
changeset
|
404 |
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
|
405 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
406 |
|
5692 | 407 |
(* print (ast) translations *) |
408 |
||
42254 | 409 |
fun apply_tr' tab c ctxt T args = |
410 |
let |
|
411 |
val fns = map fst (Symtab.lookup_list tab c); |
|
412 |
fun app_first [] = raise Match |
|
413 |
| app_first (f :: fs) = f ctxt T args handle Match => app_first fs; |
|
414 |
in app_first fns end; |
|
415 |
||
416 |
fun apply_ast_tr' tab c ctxt args = |
|
417 |
let |
|
418 |
val fns = map fst (Symtab.lookup_list tab c); |
|
419 |
fun app_first [] = raise Match |
|
420 |
| app_first (f :: fs) = f ctxt args handle Match => app_first fs; |
|
421 |
in app_first fns end; |
|
42253 | 422 |
|
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
423 |
fun update_tr'tab trfuns = fold_rev (Symtab.update_list Syntax_Ext.eq_trfun) trfuns; |
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
424 |
fun remove_tr'tab trfuns = fold (Symtab.remove_list Syntax_Ext.eq_trfun) trfuns; |
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
425 |
fun merge_tr'tabs tab1 tab2 = Symtab.merge_list Syntax_Ext.eq_trfun (tab1, tab2); |
5692 | 426 |
|
427 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
428 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
429 |
(** tables of translation rules **) |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
430 |
|
5692 | 431 |
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
|
432 |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19375
diff
changeset
|
433 |
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
|
434 |
|
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
435 |
val update_ruletab = fold_rev (fn r => Symtab.update_list (op =) (Ast.head_of_rule r, r)); |
18931 | 436 |
val remove_ruletab = fold (fn r => Symtab.remove_list (op =) (Ast.head_of_rule r, r)); |
437 |
fun merge_ruletabs tab1 tab2 = Symtab.merge_list (op =) (tab1, tab2); |
|
0 | 438 |
|
439 |
||
440 |
||
441 |
(** datatype syntax **) |
|
442 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
443 |
datatype syntax = |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
444 |
Syntax of { |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
445 |
input: Syntax_Ext.xprod list, |
4703 | 446 |
lexicon: Scan.lexicon, |
5692 | 447 |
gram: Parser.gram, |
42298
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
448 |
consts: string Symtab.table, |
2913 | 449 |
prmodes: string list, |
21772 | 450 |
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
|
451 |
parse_ruletab: ruletab, |
21772 | 452 |
parse_trtab: ((Proof.context -> term list -> term) * stamp) Symtab.table, |
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
453 |
print_trtab: ((Proof.context -> 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
|
454 |
print_ruletab: ruletab, |
21772 | 455 |
print_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) list Symtab.table, |
17079 | 456 |
prtabs: Printer.prtabs} * stamp; |
0 | 457 |
|
17079 | 458 |
fun eq_syntax (Syntax (_, s1), Syntax (_, s2)) = s1 = s2; |
459 |
||
42298
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
460 |
fun lookup_const (Syntax ({consts, ...}, _)) = Symtab.lookup consts; |
17079 | 461 |
fun is_keyword (Syntax ({lexicon, ...}, _)) = Scan.is_literal lexicon o Symbol.explode; |
42251 | 462 |
fun tokenize (Syntax ({lexicon, ...}, _)) = Lexicon.tokenize lexicon; |
463 |
fun parse ctxt (Syntax ({gram, ...}, _)) = Parser.parse ctxt gram; |
|
14687 | 464 |
|
42253 | 465 |
fun parse_ast_translation (Syntax ({parse_ast_trtab, ...}, _)) = lookup_tr parse_ast_trtab; |
466 |
fun parse_translation (Syntax ({parse_trtab, ...}, _)) = lookup_tr parse_trtab; |
|
42255 | 467 |
fun parse_rules (Syntax ({parse_ruletab, ...}, _)) = Symtab.lookup_list parse_ruletab; |
42254 | 468 |
fun print_translation (Syntax ({print_trtab, ...}, _)) = apply_tr' print_trtab; |
42255 | 469 |
fun print_rules (Syntax ({print_ruletab, ...}, _)) = Symtab.lookup_list print_ruletab; |
42254 | 470 |
fun print_ast_translation (Syntax ({print_ast_trtab, ...}, _)) = apply_ast_tr' print_ast_trtab; |
42253 | 471 |
|
42255 | 472 |
fun prtabs (Syntax ({prtabs, ...}, _)) = prtabs; |
473 |
||
20784 | 474 |
type mode = string * bool; |
24970 | 475 |
val mode_default = ("", true); |
37146
f652333bbf8e
renamed structure PrintMode to Print_Mode, keeping the old name as legacy alias for some time;
wenzelm
parents:
36739
diff
changeset
|
476 |
val mode_input = (Print_Mode.input, true); |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
477 |
|
18 | 478 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
479 |
(* empty_syntax *) |
18 | 480 |
|
17079 | 481 |
val empty_syntax = Syntax |
482 |
({input = [], |
|
4703 | 483 |
lexicon = Scan.empty_lexicon, |
5692 | 484 |
gram = Parser.empty_gram, |
42298
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
485 |
consts = Symtab.empty, |
2913 | 486 |
prmodes = [], |
5692 | 487 |
parse_ast_trtab = Symtab.empty, |
488 |
parse_ruletab = Symtab.empty, |
|
489 |
parse_trtab = Symtab.empty, |
|
490 |
print_trtab = Symtab.empty, |
|
491 |
print_ruletab = Symtab.empty, |
|
492 |
print_ast_trtab = Symtab.empty, |
|
17079 | 493 |
prtabs = Printer.empty_prtabs}, stamp ()); |
167 | 494 |
|
495 |
||
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
496 |
(* update_syntax *) |
167 | 497 |
|
42298
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
498 |
fun update_const (c, b) tab = |
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
499 |
if c = "" orelse (b = "" andalso (Lexicon.is_marked c orelse Symtab.defined tab c)) |
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
500 |
then tab |
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
501 |
else Symtab.update (c, b) tab; |
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
502 |
|
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
503 |
fun update_syntax (mode, inout) syn_ext (Syntax (tabs, _)) = |
167 | 504 |
let |
42268 | 505 |
val {input, lexicon, gram, consts = consts1, prmodes, parse_ast_trtab, parse_ruletab, |
506 |
parse_trtab, print_trtab, print_ruletab, print_ast_trtab, prtabs} = tabs; |
|
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
507 |
val Syntax_Ext.Syn_Ext {xprods, consts = consts2, parse_ast_translation, |
42268 | 508 |
parse_rules, parse_translation, print_translation, print_rules, |
509 |
print_ast_translation} = syn_ext; |
|
36208
74c5e6e3c1d3
update_syntax: add new productions only once, to allow repeated local notation, for example;
wenzelm
parents:
35668
diff
changeset
|
510 |
val new_xprods = |
74c5e6e3c1d3
update_syntax: add new productions only once, to allow repeated local notation, for example;
wenzelm
parents:
35668
diff
changeset
|
511 |
if inout then distinct (op =) (filter_out (member (op =) input) xprods) else []; |
19546
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents:
19482
diff
changeset
|
512 |
fun if_inout xs = if inout then xs else []; |
167 | 513 |
in |
17079 | 514 |
Syntax |
36208
74c5e6e3c1d3
update_syntax: add new productions only once, to allow repeated local notation, for example;
wenzelm
parents:
35668
diff
changeset
|
515 |
({input = new_xprods @ input, |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
516 |
lexicon = fold Scan.extend_lexicon (Syntax_Ext.delims_of new_xprods) lexicon, |
37684 | 517 |
gram = Parser.extend_gram new_xprods gram, |
42298
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
518 |
consts = fold update_const consts2 consts1, |
42268 | 519 |
prmodes = insert (op =) mode prmodes, |
167 | 520 |
parse_ast_trtab = |
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
521 |
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
|
522 |
parse_ruletab = update_ruletab (if_inout parse_rules) parse_ruletab, |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
523 |
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
|
524 |
print_trtab = update_tr'tab print_translation print_trtab, |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
525 |
print_ruletab = update_ruletab print_rules print_ruletab, |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
526 |
print_ast_trtab = update_tr'tab print_ast_translation print_ast_trtab, |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
527 |
prtabs = Printer.update_prtabs mode xprods prtabs}, stamp ()) |
18 | 528 |
end; |
529 |
||
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
530 |
|
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
531 |
(* remove_syntax *) |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
532 |
|
17079 | 533 |
fun remove_syntax (mode, inout) syn_ext (Syntax (tabs, _)) = |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
534 |
let |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
535 |
val Syntax_Ext.Syn_Ext {xprods, consts = _, parse_ast_translation, parse_rules, |
42268 | 536 |
parse_translation, print_translation, print_rules, print_ast_translation} = syn_ext; |
537 |
val {input, lexicon, gram, consts, prmodes, parse_ast_trtab, parse_ruletab, |
|
538 |
parse_trtab, print_trtab, print_ruletab, print_ast_trtab, prtabs} = tabs; |
|
19300 | 539 |
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
|
540 |
val changed = length input <> length input'; |
19546
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents:
19482
diff
changeset
|
541 |
fun if_inout xs = if inout then xs else []; |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
542 |
in |
17079 | 543 |
Syntax |
544 |
({input = input', |
|
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
545 |
lexicon = if changed then Scan.make_lexicon (Syntax_Ext.delims_of input') else lexicon, |
42217 | 546 |
gram = if changed then Parser.extend_gram input' Parser.empty_gram else gram, |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
547 |
consts = consts, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
548 |
prmodes = prmodes, |
19546
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents:
19482
diff
changeset
|
549 |
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
|
550 |
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
|
551 |
parse_trtab = remove_trtab (if_inout parse_translation) parse_trtab, |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
552 |
print_trtab = remove_tr'tab print_translation print_trtab, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
553 |
print_ruletab = remove_ruletab print_rules print_ruletab, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
554 |
print_ast_trtab = remove_tr'tab print_ast_translation print_ast_trtab, |
17079 | 555 |
prtabs = Printer.remove_prtabs mode xprods prtabs}, stamp ()) |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
556 |
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
|
557 |
|
18 | 558 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
559 |
(* merge_syntaxes *) |
0 | 560 |
|
17079 | 561 |
fun merge_syntaxes (Syntax (tabs1, _)) (Syntax (tabs2, _)) = |
0 | 562 |
let |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
563 |
val {input = input1, lexicon = lexicon1, gram = gram1, consts = consts1, |
42268 | 564 |
prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1, parse_ruletab = parse_ruletab1, |
565 |
parse_trtab = parse_trtab1, print_trtab = print_trtab1, print_ruletab = print_ruletab1, |
|
566 |
print_ast_trtab = print_ast_trtab1, prtabs = prtabs1} = tabs1; |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
567 |
|
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
568 |
val {input = input2, lexicon = lexicon2, gram = gram2, consts = consts2, |
42268 | 569 |
prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2, parse_ruletab = parse_ruletab2, |
570 |
parse_trtab = parse_trtab2, print_trtab = print_trtab2, print_ruletab = print_ruletab2, |
|
571 |
print_ast_trtab = print_ast_trtab2, prtabs = prtabs2} = tabs2; |
|
0 | 572 |
in |
17079 | 573 |
Syntax |
18921 | 574 |
({input = Library.merge (op =) (input1, input2), |
27768 | 575 |
lexicon = Scan.merge_lexicons (lexicon1, lexicon2), |
37684 | 576 |
gram = Parser.merge_gram (gram1, gram2), |
42298
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
577 |
consts = Symtab.merge (K true) (consts1, consts2), |
18921 | 578 |
prmodes = Library.merge (op =) (prmodes1, prmodes2), |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
579 |
parse_ast_trtab = |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
580 |
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
|
581 |
parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2, |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
582 |
parse_trtab = merge_trtabs "parse translation" parse_trtab1 parse_trtab2, |
5692 | 583 |
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
|
584 |
print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2, |
5692 | 585 |
print_ast_trtab = merge_tr'tabs print_ast_trtab1 print_ast_trtab2, |
17079 | 586 |
prtabs = Printer.merge_prtabs prtabs1 prtabs2}, stamp ()) |
0 | 587 |
end; |
588 |
||
589 |
||
18720 | 590 |
(* basic syntax *) |
260 | 591 |
|
42294 | 592 |
val token_markers = |
593 |
["_tfree", "_tvar", "_free", "_bound", "_var", "_numeral", "_inner_string"]; |
|
260 | 594 |
|
18720 | 595 |
val basic_nonterms = |
42293
6cca0343ea48
renamed sprop "prop#" to "prop'" -- proper identifier;
wenzelm
parents:
42290
diff
changeset
|
596 |
(Lexicon.terminals @ ["logic", "type", "types", "sort", "classes", |
6cca0343ea48
renamed sprop "prop#" to "prop'" -- proper identifier;
wenzelm
parents:
42290
diff
changeset
|
597 |
"args", "cargs", "pttrn", "pttrns", "idt", "idts", "aprop", "asms", |
6cca0343ea48
renamed sprop "prop#" to "prop'" -- proper identifier;
wenzelm
parents:
42290
diff
changeset
|
598 |
"any", "prop'", "num_const", "float_const", "index", "struct", |
6cca0343ea48
renamed sprop "prop#" to "prop'" -- proper identifier;
wenzelm
parents:
42290
diff
changeset
|
599 |
"id_position", "longid_position", "type_name", "class_name"]); |
18720 | 600 |
|
0 | 601 |
|
4887 | 602 |
|
15759 | 603 |
(** print syntax **) |
604 |
||
605 |
local |
|
0 | 606 |
|
260 | 607 |
fun pretty_strs_qs name strs = |
28840 | 608 |
Pretty.strs (name :: map quote (sort_strings strs)); |
0 | 609 |
|
17079 | 610 |
fun pretty_gram (Syntax (tabs, _)) = |
0 | 611 |
let |
32784 | 612 |
val {lexicon, prmodes, gram, ...} = tabs; |
28375 | 613 |
val prmodes' = sort_strings (filter_out (fn s => s = "") prmodes); |
0 | 614 |
in |
8720 | 615 |
[pretty_strs_qs "lexicon:" (Scan.dest_lexicon lexicon), |
616 |
Pretty.big_list "prods:" (Parser.pretty_gram gram), |
|
617 |
pretty_strs_qs "print modes:" prmodes'] |
|
0 | 618 |
end; |
619 |
||
17079 | 620 |
fun pretty_trans (Syntax (tabs, _)) = |
0 | 621 |
let |
42298
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
622 |
fun pretty_tab name tab = |
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
623 |
pretty_strs_qs name (sort_strings (Symtab.keys tab)); |
0 | 624 |
|
260 | 625 |
fun pretty_ruletab name tab = |
5692 | 626 |
Pretty.big_list name (map Ast.pretty_rule (dest_ruletab tab)); |
0 | 627 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
628 |
val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, |
42268 | 629 |
print_ruletab, print_ast_trtab, ...} = tabs; |
0 | 630 |
in |
42298
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
631 |
[pretty_tab "consts:" consts, |
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
632 |
pretty_tab "parse_ast_translation:" parse_ast_trtab, |
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
633 |
pretty_ruletab "parse_rules:" parse_ruletab, |
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
634 |
pretty_tab "parse_translation:" parse_trtab, |
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
635 |
pretty_tab "print_translation:" print_trtab, |
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
636 |
pretty_ruletab "print_rules:" print_ruletab, |
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42297
diff
changeset
|
637 |
pretty_tab "print_ast_translation:" print_ast_trtab] |
0 | 638 |
end; |
639 |
||
15759 | 640 |
in |
0 | 641 |
|
15759 | 642 |
fun print_gram syn = Pretty.writeln (Pretty.chunks (pretty_gram syn)); |
643 |
fun print_trans syn = Pretty.writeln (Pretty.chunks (pretty_trans syn)); |
|
644 |
fun print_syntax syn = Pretty.writeln (Pretty.chunks (pretty_gram syn @ pretty_trans syn)); |
|
0 | 645 |
|
15759 | 646 |
end; |
0 | 647 |
|
648 |
||
26951
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
649 |
(* reconstructing infixes -- educated guessing *) |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
650 |
|
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
651 |
fun guess_infix (Syntax ({gram, ...}, _)) c = |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
652 |
(case Parser.guess_infix_lr gram c of |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
653 |
SOME (s, l, r, j) => SOME |
35130 | 654 |
(if l then Mixfix.Infixl (s, j) |
655 |
else if r then Mixfix.Infixr (s, j) |
|
656 |
else Mixfix.Infix (s, j)) |
|
26951
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
657 |
| NONE => NONE); |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
658 |
|
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
659 |
|
0 | 660 |
|
1158 | 661 |
(** prepare translation rules **) |
662 |
||
42204 | 663 |
(* rules *) |
664 |
||
1158 | 665 |
datatype 'a trrule = |
42204 | 666 |
Parse_Rule of 'a * 'a | |
667 |
Print_Rule of 'a * 'a | |
|
668 |
Parse_Print_Rule of 'a * 'a; |
|
888
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
669 |
|
42204 | 670 |
fun map_trrule f (Parse_Rule (x, y)) = Parse_Rule (f x, f y) |
671 |
| map_trrule f (Print_Rule (x, y)) = Print_Rule (f x, f y) |
|
672 |
| map_trrule f (Parse_Print_Rule (x, y)) = Parse_Print_Rule (f x, f y); |
|
1158 | 673 |
|
42204 | 674 |
fun parse_rule (Parse_Rule pats) = SOME pats |
675 |
| parse_rule (Print_Rule _) = NONE |
|
676 |
| parse_rule (Parse_Print_Rule pats) = SOME pats; |
|
1158 | 677 |
|
42204 | 678 |
fun print_rule (Parse_Rule _) = NONE |
679 |
| print_rule (Print_Rule pats) = SOME (swap pats) |
|
680 |
| print_rule (Parse_Print_Rule pats) = SOME (swap pats); |
|
1158 | 681 |
|
682 |
||
42204 | 683 |
(* check_rules *) |
684 |
||
19262 | 685 |
local |
686 |
||
42048
afd11ca8e018
support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents:
42044
diff
changeset
|
687 |
fun check_rule rule = |
5692 | 688 |
(case Ast.rule_error rule of |
15531 | 689 |
SOME msg => |
1158 | 690 |
error ("Error in syntax translation rule: " ^ msg ^ "\n" ^ |
42048
afd11ca8e018
support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents:
42044
diff
changeset
|
691 |
Pretty.string_of (Ast.pretty_rule rule)) |
15531 | 692 |
| NONE => rule); |
888
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
693 |
|
42204 | 694 |
in |
695 |
||
696 |
fun check_rules rules = |
|
697 |
(map check_rule (map_filter parse_rule rules), |
|
698 |
map check_rule (map_filter print_rule rules)); |
|
699 |
||
700 |
end; |
|
701 |
||
702 |
||
18 | 703 |
|
19262 | 704 |
(** modify syntax **) |
383
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents:
330
diff
changeset
|
705 |
|
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
706 |
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
|
707 |
|
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
708 |
val update_trfuns = ext_syntax Syntax_Ext.syn_ext_trfuns; |
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
709 |
val update_advanced_trfuns = ext_syntax Syntax_Ext.syn_ext_advanced_trfuns; |
5692 | 710 |
|
35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
35394
diff
changeset
|
711 |
fun update_type_gram add prmode decls = |
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
35394
diff
changeset
|
712 |
(if add then update_syntax else remove_syntax) prmode (Mixfix.syn_ext_types decls); |
25387 | 713 |
|
35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
35394
diff
changeset
|
714 |
fun update_const_gram add is_logtype prmode decls = |
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
35394
diff
changeset
|
715 |
(if add then update_syntax else remove_syntax) prmode (Mixfix.syn_ext_consts is_logtype decls); |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
716 |
|
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
717 |
val update_trrules = ext_syntax Syntax_Ext.syn_ext_rules o check_rules; |
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42287
diff
changeset
|
718 |
val remove_trrules = remove_syntax mode_default o Syntax_Ext.syn_ext_rules o check_rules; |
5692 | 719 |
|
0 | 720 |
end; |
5692 | 721 |