author | clasohm |
Wed, 29 Nov 1995 16:47:38 +0100 | |
changeset 1371 | 2f97fc253763 |
parent 1321 | 9a6e7bd2bfaf |
child 1377 | f800f533aa83 |
permissions | -rw-r--r-- |
389 | 1 |
(* Title: Pure/Thy/thy_parse.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
4 |
||
5 |
The parser for theory files. |
|
6 |
*) |
|
7 |
||
8 |
infix 5 -- --$$ $$-- ^^; |
|
9 |
infix 3 >>; |
|
10 |
infix 0 ||; |
|
11 |
||
12 |
signature THY_PARSE = |
|
13 |
sig |
|
14 |
type token |
|
15 |
val !! : ('a -> 'b * 'c) -> 'a -> 'b * 'c |
|
16 |
val >> : ('a -> 'b * 'c) * ('b -> 'd) -> 'a -> 'd * 'c |
|
17 |
val || : ('a -> 'b) * ('a -> 'b) -> 'a -> 'b |
|
18 |
val -- : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> ('b * 'd) * 'e |
|
19 |
val ^^ : ('a -> string * 'b) * ('b -> string * 'c) -> 'a -> string * 'c |
|
20 |
val $$ : string -> token list -> string * token list |
|
21 |
val $$-- : string * (token list -> 'b * 'c) -> token list -> 'b * 'c |
|
22 |
val --$$ : ('a -> 'b * token list) * string -> 'a -> 'b * token list |
|
23 |
val ident: token list -> string * token list |
|
24 |
val long_ident: token list -> string * token list |
|
25 |
val long_id: token list -> string * token list |
|
26 |
val type_var: token list -> string * token list |
|
636 | 27 |
val type_args: token list -> string list * token list |
389 | 28 |
val nat: token list -> string * token list |
29 |
val string: token list -> string * token list |
|
30 |
val verbatim: token list -> string * token list |
|
31 |
val empty: 'a -> 'b list * 'a |
|
32 |
val optional: ('a -> 'b * 'a) -> 'b -> 'a -> 'b * 'a |
|
33 |
val repeat: ('a -> 'b * 'a) -> 'a -> 'b list * 'a |
|
34 |
val repeat1: ('a -> 'b * 'a) -> 'a -> 'b list * 'a |
|
35 |
val enum: string -> (token list -> 'a * token list) |
|
36 |
-> token list -> 'a list * token list |
|
37 |
val enum1: string -> (token list -> 'a * token list) |
|
38 |
-> token list -> 'a list * token list |
|
39 |
val list: (token list -> 'a * token list) |
|
40 |
-> token list -> 'a list * token list |
|
41 |
val list1: (token list -> 'a * token list) |
|
42 |
-> token list -> 'a list * token list |
|
43 |
val name: token list -> string * token list |
|
44 |
val sort: token list -> string * token list |
|
451 | 45 |
val opt_infix: token list -> string * token list |
46 |
val opt_mixfix: token list -> string * token list |
|
636 | 47 |
val opt_witness: token list -> string * token list |
389 | 48 |
type syntax |
49 |
val make_syntax: string list -> |
|
50 |
(string * (token list -> (string * string) * token list)) list -> syntax |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
51 |
val parse_thy: syntax -> string -> string -> string |
389 | 52 |
val section: string -> string -> (token list -> string * token list) |
53 |
-> (string * (token list -> (string * string) * token list)) |
|
54 |
val axm_section: string -> string |
|
55 |
-> (token list -> (string * string list) * token list) |
|
56 |
-> (string * (token list -> (string * string) * token list)) |
|
57 |
val pure_keywords: string list |
|
58 |
val pure_sections: |
|
59 |
(string * (token list -> (string * string) * token list)) list |
|
570
6333c181a3f7
Pure/Thy/thy_parse/THY_PARSE: deleted duplicate specifications of parens,
lcp
parents:
558
diff
changeset
|
60 |
(*items for building strings*) |
710 | 61 |
val cat: string -> string -> string |
656 | 62 |
val parens: string -> string |
63 |
val brackets: string -> string |
|
64 |
val mk_list: string list -> string |
|
65 |
val mk_big_list: string list -> string |
|
66 |
val mk_pair: string * string -> string |
|
67 |
val mk_triple: string * string * string -> string |
|
68 |
val strip_quotes: string -> string |
|
389 | 69 |
end; |
70 |
||
1321
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
71 |
functor ThyParseFun(structure Symtab: SYMTAB and ThyScan: THY_SCAN): THY_PARSE= |
389 | 72 |
struct |
73 |
||
74 |
open ThyScan; |
|
75 |
||
76 |
||
77 |
(** parser toolbox **) |
|
78 |
||
79 |
type token = token_kind * string * int; |
|
80 |
||
81 |
||
82 |
(* errors *) |
|
83 |
||
84 |
exception SYNTAX_ERROR of string * string * int; |
|
85 |
||
86 |
fun syn_err s1 s2 n = raise SYNTAX_ERROR (s1, s2, n); |
|
87 |
||
88 |
fun eof_err () = error "Unexpected end-of-file"; |
|
89 |
||
570
6333c181a3f7
Pure/Thy/thy_parse/THY_PARSE: deleted duplicate specifications of parens,
lcp
parents:
558
diff
changeset
|
90 |
(*Similar to Prolog's cut: reports any syntax error instead of backtracking |
6333c181a3f7
Pure/Thy/thy_parse/THY_PARSE: deleted duplicate specifications of parens,
lcp
parents:
558
diff
changeset
|
91 |
through a superior || *) |
389 | 92 |
fun !! parse toks = parse toks |
93 |
handle SYNTAX_ERROR (s1, s2, n) => error ("Syntax error on line " ^ |
|
94 |
string_of_int n ^ ": " ^ s1 ^ " expected and " ^ s2 ^ " was found"); |
|
95 |
||
96 |
||
97 |
(* parser combinators *) |
|
98 |
||
99 |
fun (parse >> f) toks = apfst f (parse toks); |
|
100 |
||
101 |
fun (parse1 || parse2) toks = |
|
102 |
parse1 toks handle SYNTAX_ERROR _ => parse2 toks; |
|
103 |
||
104 |
fun (parse1 -- parse2) toks = |
|
105 |
let |
|
106 |
val (x, toks') = parse1 toks; |
|
107 |
val (y, toks'') = parse2 toks'; |
|
108 |
in |
|
109 |
((x, y), toks'') |
|
110 |
end; |
|
111 |
||
112 |
fun (parse1 ^^ parse2) = parse1 -- parse2 >> op ^; |
|
113 |
||
114 |
||
115 |
(* generic parsers *) |
|
116 |
||
117 |
fun $$ a ((k, b, n) :: toks) = |
|
118 |
if k = Keyword andalso a = b then (a, toks) |
|
119 |
else syn_err (quote a) (quote b) n |
|
120 |
| $$ _ [] = eof_err (); |
|
121 |
||
122 |
fun (a $$-- parse) = $$ a -- parse >> #2; |
|
123 |
||
124 |
fun (parse --$$ a) = parse -- $$ a >> #1; |
|
125 |
||
126 |
||
127 |
fun kind k1 ((k2, s, n) :: toks) = |
|
128 |
if k1 = k2 then (s, toks) |
|
129 |
else syn_err (name_of_kind k1) (quote s) n |
|
130 |
| kind _ [] = eof_err (); |
|
131 |
||
132 |
val ident = kind Ident; |
|
133 |
val long_ident = kind LongIdent; |
|
134 |
val long_id = ident || long_ident; |
|
135 |
val type_var = kind TypeVar >> quote; |
|
136 |
val nat = kind Nat; |
|
137 |
val string = kind String; |
|
138 |
val verbatim = kind Verbatim; |
|
139 |
val eof = kind EOF; |
|
140 |
||
141 |
fun empty toks = ([], toks); |
|
142 |
||
143 |
fun optional parse def = parse || empty >> K def; |
|
144 |
||
145 |
fun repeat parse toks = (parse -- repeat parse >> op :: || empty) toks; |
|
146 |
fun repeat1 parse = parse -- repeat parse >> op ::; |
|
147 |
||
148 |
fun enum1 sep parse = parse -- repeat (sep $$-- parse) >> op ::; |
|
149 |
fun enum sep parse = enum1 sep parse || empty; |
|
150 |
||
151 |
val list = enum ","; |
|
152 |
val list1 = enum1 ","; |
|
153 |
||
154 |
||
155 |
(** theory parsers **) |
|
156 |
||
157 |
(* misc utilities *) |
|
158 |
||
159 |
fun cat s1 s2 = s1 ^ " " ^ s2; |
|
160 |
||
558 | 161 |
val parens = enclose "(" ")"; |
162 |
val brackets = enclose "[" "]"; |
|
389 | 163 |
|
164 |
val mk_list = brackets o commas; |
|
165 |
val mk_big_list = brackets o space_implode ",\n "; |
|
166 |
||
558 | 167 |
fun mk_pair (x, y) = parens (commas [x, y]); |
168 |
fun mk_triple (x, y, z) = parens (commas [x, y, z]); |
|
389 | 169 |
fun mk_triple1 ((x, y), z) = mk_triple (x, y, z); |
170 |
fun mk_triple2 (x, (y, z)) = mk_triple (x, y, z); |
|
171 |
||
172 |
val split_decls = flat o map (fn (xs, y) => map (rpair y) xs); |
|
173 |
||
174 |
fun strip_quotes str = |
|
175 |
implode (tl (take (size str - 1, explode str))); |
|
176 |
||
177 |
||
178 |
(* names *) |
|
179 |
||
180 |
val name = ident >> quote || string; |
|
181 |
val names = list name; |
|
182 |
val names1 = list1 name; |
|
183 |
val name_list = names >> mk_list; |
|
184 |
val name_list1 = names1 >> mk_list; |
|
185 |
||
186 |
||
187 |
(* classes *) |
|
188 |
||
189 |
val subclass = name -- optional ("<" $$-- !! name_list1) "[]"; |
|
190 |
||
558 | 191 |
val class_decls = repeat1 (subclass >> mk_pair) >> mk_big_list; |
389 | 192 |
|
193 |
||
194 |
(* arities *) |
|
195 |
||
196 |
val sort = |
|
197 |
name >> brackets || |
|
198 |
"{" $$-- name_list --$$ "}"; |
|
199 |
||
200 |
val sort_list1 = list1 sort >> mk_list; |
|
201 |
||
202 |
||
203 |
val arity = optional ("(" $$-- !! (sort_list1 --$$")")) "[]" -- sort; |
|
204 |
||
205 |
val arity_decls = repeat1 (names1 --$$ "::" -- !! arity) |
|
206 |
>> (mk_big_list o map mk_triple2 o split_decls); |
|
207 |
||
208 |
||
209 |
(* mixfix annotations *) |
|
210 |
||
211 |
val infxl = "infixl" $$-- !! nat >> cat "Infixl"; |
|
212 |
val infxr = "infixr" $$-- !! nat >> cat "Infixr"; |
|
213 |
||
889 | 214 |
val binder = "binder" $$-- |
1027
651637377289
(binder "Q" p) generates Binder("Q",p,p); it used to be Binder("Q",0,p).
nipkow
parents:
889
diff
changeset
|
215 |
!! (string -- ( ("[" $$-- nat --$$ "]") -- nat |
651637377289
(binder "Q" p) generates Binder("Q",p,p); it used to be Binder("Q",0,p).
nipkow
parents:
889
diff
changeset
|
216 |
|| nat >> (fn n => (n,n)) |
651637377289
(binder "Q" p) generates Binder("Q",p,p); it used to be Binder("Q",0,p).
nipkow
parents:
889
diff
changeset
|
217 |
) ) |
651637377289
(binder "Q" p) generates Binder("Q",p,p); it used to be Binder("Q",0,p).
nipkow
parents:
889
diff
changeset
|
218 |
>> (cat "Binder" o mk_triple2); |
389 | 219 |
|
220 |
val opt_pris = optional ("[" $$-- !! (list nat --$$ "]")) [] >> mk_list; |
|
221 |
||
222 |
val mixfix = string -- !! (opt_pris -- optional nat "max_pri") |
|
223 |
>> (cat "Mixfix" o mk_triple2); |
|
224 |
||
558 | 225 |
fun opt_syn fx = optional ("(" $$-- fx --$$ ")") "NoSyn"; |
389 | 226 |
|
227 |
val opt_infix = opt_syn (infxl || infxr); |
|
228 |
val opt_mixfix = opt_syn (mixfix || infxl || infxr || binder); |
|
229 |
||
230 |
||
231 |
(* types *) |
|
232 |
||
233 |
fun mk_old_type_decl ((ts, n), syn) = |
|
234 |
map (fn t => (mk_triple (t, n, syn), false)) ts; |
|
235 |
||
236 |
fun mk_type_decl (((xs, t), None), syn) = |
|
237 |
[(mk_triple (t, string_of_int (length xs), syn), false)] |
|
238 |
| mk_type_decl (((xs, t), Some rhs), syn) = |
|
558 | 239 |
[(parens (commas [t, mk_list xs, rhs, syn]), true)]; |
389 | 240 |
|
241 |
fun mk_type_decls tys = |
|
414 | 242 |
"|> add_types\n" ^ mk_big_list (keyfilter tys false) ^ "\n\n\ |
243 |
\|> add_tyabbrs\n" ^ mk_big_list (keyfilter tys true); |
|
389 | 244 |
|
245 |
||
425 | 246 |
val old_type_decl = names1 -- nat -- opt_infix >> mk_old_type_decl; |
389 | 247 |
|
248 |
val type_args = |
|
249 |
type_var >> (fn x => [x]) || |
|
250 |
"(" $$-- !! (list1 type_var --$$ ")") || |
|
251 |
empty >> K []; |
|
252 |
||
253 |
val type_decl = type_args -- name -- optional ("=" $$-- !! string >> Some) None |
|
254 |
-- opt_infix >> mk_type_decl; |
|
255 |
||
1321
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
256 |
val type_decls = repeat1 (old_type_decl || type_decl) >> |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
257 |
(mk_type_decls o flat); |
389 | 258 |
|
259 |
||
260 |
(* consts *) |
|
261 |
||
1371 | 262 |
val simple_type = ident || |
263 |
(kind TypeVar -- optional ("::" $$-- ident >> cat "::") "" >> |
|
264 |
(fn (tv, cl) => tv ^ cl)); |
|
1321
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
265 |
|
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
266 |
(*Types with type arguments, like e.g. "'a list list"*) |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
267 |
fun complex_type toks = |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
268 |
let val typ = simple_type || "(" $$-- complex_type --$$ ")" >> parens; |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
269 |
val typ2 = complex_type || "(" $$-- complex_type --$$ ")" >> parens; |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
270 |
in |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
271 |
(typ -- repeat ident >> (fn (t, ts) => space_implode " " (t::ts)) || |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
272 |
"(" $$-- !! (list1 typ2) --$$ ")" -- !! (repeat1 ident) >> |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
273 |
(fn (tas, ts) => space_implode " " (parens (commas tas) :: ts))) toks |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
274 |
end; |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
275 |
|
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
276 |
(*Const type which is limited at the end by "=>", ")", "]" or ","*) |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
277 |
fun param_type toks = |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
278 |
((complex_type || "(" $$-- param_type --$$ ")" >> parens || |
1371 | 279 |
"[" $$-- (list1 param_type) --$$ "]" >> mk_list) -- |
1321
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
280 |
repeat ("=>" $$-- param_type >> cat " =>") >> |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
281 |
(fn (t, ts) => t ^ implode ts)) toks; |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
282 |
|
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
283 |
(*Parse an identifier, but only if it is not followed by colons or a comma*) |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
284 |
fun ident_no_colon [] = eof_err() |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
285 |
| ident_no_colon ((Ident, s, n) :: (rest as (Keyword, s2, n2) :: toks)) = |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
286 |
if s2 = "::" orelse s2 = "," then |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
287 |
syn_err (name_of_kind Ident) (quote s2) n2 |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
288 |
else (s, rest) |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
289 |
| ident_no_colon ((Ident, s, n) :: toks) = (s, toks) |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
290 |
| ident_no_colon ((k, s, n) :: _) = |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
291 |
syn_err (name_of_kind Ident) (quote s) n; |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
292 |
|
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
293 |
(*Type after last "=>"*) |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
294 |
val appl_last = |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
295 |
(simple_type || "(" $$-- param_type --$$ ")" >> parens) -- |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
296 |
repeat ident_no_colon >> (fn (t, ts) => space_implode " " (t::ts)); |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
297 |
|
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
298 |
(*Type specified before "=>"*) |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
299 |
val appl_param = |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
300 |
(complex_type || "(" $$-- param_type --$$ ")" >> parens || |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
301 |
"[" $$-- !! (list1 param_type) --$$ "]" >> (brackets o commas)) --$$ "=>"; |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
302 |
|
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
303 |
(*Top level const type*) |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
304 |
val const_type = repeat appl_param -- appl_last >> |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
305 |
(fn (ts, t) => space_implode " => " (ts@[t])); |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
306 |
|
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
307 |
val const_decls = repeat1 (names1 --$$ "::" -- !! |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
308 |
((string || const_type>>quote) -- opt_mixfix)) >> |
9a6e7bd2bfaf
types in consts section of .thy files can now be specified without quotes
clasohm
parents:
1250
diff
changeset
|
309 |
(mk_big_list o map mk_triple2 o split_decls); |
389 | 310 |
|
311 |
||
312 |
(* translations *) |
|
313 |
||
314 |
val trans_pat = |
|
315 |
optional ("(" $$-- !! (name --$$ ")")) "\"logic\"" -- string >> mk_pair; |
|
316 |
||
317 |
val trans_arrow = |
|
318 |
$$ "=>" >> K " |-> " || |
|
319 |
$$ "<=" >> K " <-| " || |
|
320 |
$$ "==" >> K " <-> "; |
|
321 |
||
322 |
val trans_decls = repeat1 (trans_pat ^^ !! (trans_arrow ^^ trans_pat)) |
|
323 |
>> mk_big_list; |
|
324 |
||
325 |
||
326 |
(* ML translations *) |
|
327 |
||
328 |
val trfun_defs = |
|
329 |
" val parse_ast_translation = [];\n\ |
|
330 |
\ val parse_translation = [];\n\ |
|
331 |
\ val print_translation = [];\n\ |
|
332 |
\ val print_ast_translation = [];"; |
|
333 |
||
334 |
val trfun_args = |
|
335 |
"(parse_ast_translation, parse_translation, \ |
|
336 |
\print_translation, print_ast_translation)"; |
|
337 |
||
338 |
fun mk_mltrans txt = |
|
339 |
"let\n" |
|
340 |
^ trfun_defs ^ "\n" |
|
341 |
^ txt ^ "\n\ |
|
342 |
\in\n\ |
|
343 |
\ " ^ trfun_args ^ "\n\ |
|
344 |
\end"; |
|
345 |
||
346 |
val mltrans = verbatim >> mk_mltrans; |
|
347 |
||
348 |
||
349 |
(* axioms *) |
|
350 |
||
351 |
val mk_axms = mk_big_list o map (mk_pair o apfst quote); |
|
352 |
||
353 |
fun mk_axiom_decls axms = (mk_axms axms, map fst axms); |
|
354 |
||
355 |
val axiom_decls = repeat1 (ident -- !! string) >> mk_axiom_decls; |
|
356 |
||
357 |
||
358 |
(* axclass *) |
|
359 |
||
360 |
fun mk_axclass_decl ((c, cs), axms) = |
|
361 |
(mk_pair (c, cs) ^ "\n" ^ mk_axms axms, |
|
362 |
(strip_quotes c ^ "I") :: map fst axms); |
|
363 |
||
364 |
val axclass_decl = subclass -- repeat (ident -- !! string) >> mk_axclass_decl; |
|
365 |
||
366 |
||
451 | 367 |
(* instance *) |
389 | 368 |
|
425 | 369 |
fun mk_witness (axths, opt_tac) = |
389 | 370 |
mk_list (keyfilter axths false) ^ "\n" ^ |
371 |
mk_list (keyfilter axths true) ^ "\n" ^ |
|
372 |
opt_tac; |
|
373 |
||
374 |
val axm_or_thm = |
|
375 |
string >> rpair false || |
|
376 |
long_id >> rpair true; |
|
377 |
||
451 | 378 |
|
425 | 379 |
val opt_witness = |
389 | 380 |
optional ("(" $$-- list1 axm_or_thm --$$ ")") [] -- |
558 | 381 |
optional (verbatim >> (parens o cat "Some" o parens)) "None" |
425 | 382 |
>> mk_witness; |
383 |
||
384 |
val instance_decl = |
|
636 | 385 |
(name --$$ "<" -- name >> (pair "|> AxClass.add_inst_subclass" o mk_pair) || |
386 |
name --$$ "::" -- arity >> (pair "|> AxClass.add_inst_arity" o mk_triple2)) |
|
451 | 387 |
-- opt_witness |
777 | 388 |
>> (fn ((x, y), z) => (cat_lines [x, y, z])); |
389 | 389 |
|
390 |
||
391 |
||
392 |
(** theory syntax **) |
|
393 |
||
394 |
type syntax = |
|
395 |
lexicon * (token list -> (string * string) * token list) Symtab.table; |
|
396 |
||
397 |
fun make_syntax keywords sects = |
|
451 | 398 |
(make_lexicon (map fst sects @ keywords), |
399 |
Symtab.make sects handle Symtab.DUPS dups => |
|
400 |
error ("Duplicate sections in theory file syntax: " ^ commas_quote dups)); |
|
389 | 401 |
|
402 |
||
403 |
(* header *) |
|
404 |
||
405 |
fun mk_header (thy_name, bases) = |
|
586
201e115d8031
renamed base_on into mk_base and moved it to the beginning of the generated
clasohm
parents:
570
diff
changeset
|
406 |
(thy_name, "mk_base " ^ mk_list bases ^ " " ^ quote thy_name); |
389 | 407 |
|
408 |
val base = |
|
409 |
ident >> (cat "Thy" o quote) || |
|
410 |
string >> cat "File"; |
|
411 |
||
412 |
val header = ident --$$ "=" -- enum1 "+" base >> mk_header; |
|
413 |
||
414 |
||
415 |
(* extension *) |
|
416 |
||
417 |
fun mk_extension (txts, mltxt) = |
|
418 |
let |
|
419 |
val cat_sects = space_implode "\n\n" o filter_out (equal ""); |
|
420 |
val (extxts, postxts) = split_list txts; |
|
421 |
in |
|
422 |
(cat_sects extxts, cat_sects postxts, mltxt) |
|
423 |
end; |
|
424 |
||
425 |
fun sect tab ((Keyword, s, n) :: toks) = |
|
426 |
(case Symtab.lookup (tab, s) of |
|
427 |
Some parse => !! parse toks |
|
428 |
| None => syn_err "section" s n) |
|
429 |
| sect _ ((_, s, n) :: _) = syn_err "section" s n |
|
430 |
| sect _ [] = eof_err (); |
|
431 |
||
432 |
fun extension sectab = "+" $$-- !! (repeat (sect sectab) --$$ "end") -- |
|
433 |
optional ("ML" $$-- verbatim) "" >> mk_extension; |
|
434 |
||
435 |
||
436 |
(* theory definition *) |
|
437 |
||
558 | 438 |
fun mk_structure tname ((thy_name, old_thys), opt_txts) = |
439 |
if thy_name <> tname then |
|
440 |
error ("Filename \"" ^ tname ^ ".thy\" and theory name " |
|
441 |
^ quote thy_name ^ " are different") |
|
442 |
else |
|
443 |
(case opt_txts of |
|
444 |
Some (extxt, postxt, mltxt) => |
|
587
3ba470399605
renamed temporary variable 'base' to 'thy' in mk_structure
clasohm
parents:
586
diff
changeset
|
445 |
"val thy = " ^ old_thys ^ " true;\n\n\ |
586
201e115d8031
renamed base_on into mk_base and moved it to the beginning of the generated
clasohm
parents:
570
diff
changeset
|
446 |
\structure " ^ thy_name ^ " =\n\ |
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
447 |
\struct\n\ |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
448 |
\\n\ |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
449 |
\local\n" |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
450 |
^ trfun_defs ^ "\n\ |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
451 |
\in\n\ |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
452 |
\\n" |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
453 |
^ mltxt ^ "\n\ |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
454 |
\\n\ |
777 | 455 |
\val thy = thy\n\ |
456 |
\\n\ |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
457 |
\|> add_trfuns\n" |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
458 |
^ trfun_args ^ "\n\ |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
459 |
\\n" |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
460 |
^ extxt ^ "\n\ |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
461 |
\\n\ |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
462 |
\|> add_thyname " ^ quote thy_name ^ ";\n\ |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
463 |
\\n\ |
1235 | 464 |
\val _ = store_theory (thy, " ^ quote thy_name ^ ");\n\ |
777 | 465 |
\\n\ |
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
466 |
\\n" |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
467 |
^ postxt ^ "\n\ |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
468 |
\\n\ |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
469 |
\end;\n\ |
777 | 470 |
\end;\n" |
558 | 471 |
| None => |
777 | 472 |
"val thy = " ^ old_thys ^ " false;\n\ |
473 |
\\n\ |
|
586
201e115d8031
renamed base_on into mk_base and moved it to the beginning of the generated
clasohm
parents:
570
diff
changeset
|
474 |
\structure " ^ thy_name ^ " =\n\ |
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
475 |
\struct\n\ |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
476 |
\\n\ |
587
3ba470399605
renamed temporary variable 'base' to 'thy' in mk_structure
clasohm
parents:
586
diff
changeset
|
477 |
\val thy = thy\n\ |
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
478 |
\\n\ |
1235 | 479 |
\val _ = store_theory (thy, " ^ quote thy_name ^ ");\n\ |
777 | 480 |
\\n\ |
481 |
\end;\n"); |
|
389 | 482 |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
483 |
fun theory_defn sectab tname = |
389 | 484 |
header -- optional (extension sectab >> Some) None -- eof |
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
485 |
>> (mk_structure tname o #1); |
389 | 486 |
|
476
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
487 |
fun parse_thy (lex, sectab) tname str = |
836cad329311
added check for concistency of filename and theory name;
clasohm
parents:
451
diff
changeset
|
488 |
#1 (!! (theory_defn sectab tname) (tokenize lex str)); |
389 | 489 |
|
490 |
||
491 |
(* standard sections *) |
|
492 |
||
493 |
fun mk_val ax = "val " ^ ax ^ " = get_axiom thy " ^ quote ax ^ ";"; |
|
777 | 494 |
val mk_vals = cat_lines o map mk_val; |
389 | 495 |
|
777 | 496 |
fun mk_axm_sect "" (txt, axs) = (txt, mk_vals axs) |
497 |
| mk_axm_sect pretxt (txt, axs) = (pretxt ^ "\n" ^ txt, mk_vals axs); |
|
389 | 498 |
|
499 |
fun axm_section name pretxt parse = |
|
500 |
(name, parse >> mk_axm_sect pretxt); |
|
501 |
||
502 |
fun section name pretxt parse = |
|
503 |
axm_section name pretxt (parse >> rpair []); |
|
504 |
||
505 |
||
506 |
val pure_keywords = |
|
451 | 507 |
["end", "ML", "mixfix", "infixr", "infixl", "binder", "=", "+", ",", "<", |
777 | 508 |
"{", "}", "(", ")", "[", "]", "::", "==", "=>", "<="]; |
389 | 509 |
|
510 |
val pure_sections = |
|
414 | 511 |
[section "classes" "|> add_classes" class_decls, |
512 |
section "default" "|> add_defsort" sort, |
|
777 | 513 |
section "types" "" type_decls, |
414 | 514 |
section "arities" "|> add_arities" arity_decls, |
515 |
section "consts" "|> add_consts" const_decls, |
|
516 |
section "syntax" "|> add_syntax" const_decls, |
|
517 |
section "translations" "|> add_trrules" trans_decls, |
|
518 |
section "MLtrans" "|> add_trfuns" mltrans, |
|
777 | 519 |
section "MLtext" "" verbatim, |
414 | 520 |
axm_section "rules" "|> add_axioms" axiom_decls, |
558 | 521 |
axm_section "defs" "|> add_defs" axiom_decls, |
636 | 522 |
axm_section "axclass" "|> AxClass.add_axclass" axclass_decl, |
777 | 523 |
section "instance" "" instance_decl]; |
389 | 524 |
|
525 |
||
526 |
end; |