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