41 val extend_trfuns: syntax -> |
41 val extend_trfuns: syntax -> |
42 (string * (ast list -> ast)) list * |
42 (string * (ast list -> ast)) list * |
43 (string * (term list -> term)) list * |
43 (string * (term list -> term)) list * |
44 (string * (term list -> term)) list * |
44 (string * (term list -> term)) list * |
45 (string * (ast list -> ast)) list -> syntax |
45 (string * (ast list -> ast)) list -> syntax |
46 val extend_trrules: syntax -> xrule list -> syntax |
46 val extend_trrules: syntax -> |
|
47 (bool -> term list * typ -> int * term * 'a) -> xrule list -> syntax |
47 val merge_syntaxes: syntax -> syntax -> syntax |
48 val merge_syntaxes: syntax -> syntax -> syntax |
48 val type_syn: syntax |
49 val type_syn: syntax |
49 val print_gram: syntax -> unit |
50 val print_gram: syntax -> unit |
50 val print_trans: syntax -> unit |
51 val print_trans: syntax -> unit |
51 val print_syntax: syntax -> unit |
52 val print_syntax: syntax -> unit |
52 val test_read: syntax -> string -> string -> unit |
53 val test_read: syntax -> string -> string -> unit |
53 val read: syntax -> typ -> string -> term |
54 val read: syntax -> typ -> string -> term list |
54 val read_typ: syntax -> (indexname -> sort) -> string -> typ |
55 val read_typ: syntax -> (indexname -> sort) -> string -> typ |
55 val simple_read_typ: string -> typ |
56 val simple_read_typ: string -> typ |
56 val pretty_term: syntax -> term -> Pretty.T |
57 val pretty_term: syntax -> term -> Pretty.T |
57 val pretty_typ: syntax -> typ -> Pretty.T |
58 val pretty_typ: syntax -> typ -> Pretty.T |
58 val string_of_term: syntax -> term -> string |
59 val string_of_term: syntax -> term -> string |
174 print_ast_translation} = syn_ext; |
175 print_ast_translation} = syn_ext; |
175 in |
176 in |
176 Syntax { |
177 Syntax { |
177 lexicon = extend_lexicon lexicon (delims_of xprods), |
178 lexicon = extend_lexicon lexicon (delims_of xprods), |
178 roots = extend_list roots1 roots2, |
179 roots = extend_list roots1 roots2, |
179 gram = extend_gram gram xprods, |
180 gram = extend_gram gram (roots1 @ roots2) xprods, |
180 consts = consts2 union consts1, |
181 consts = consts2 union consts1, |
181 parse_ast_trtab = |
182 parse_ast_trtab = |
182 extend_trtab parse_ast_trtab parse_ast_translation "parse ast translation", |
183 extend_trtab parse_ast_trtab parse_ast_translation "parse ast translation", |
183 parse_ruletab = extend_ruletab parse_ruletab parse_rules, |
184 parse_ruletab = extend_ruletab parse_ruletab parse_rules, |
184 parse_trtab = extend_trtab parse_trtab parse_translation "parse translation", |
185 parse_trtab = extend_trtab parse_trtab parse_translation "parse translation", |
299 end; |
300 end; |
300 |
301 |
301 |
302 |
302 (* read_ast *) |
303 (* read_ast *) |
303 |
304 |
304 fun read_ast (Syntax tabs) xids root str = |
305 fun read_asts (Syntax tabs) print_msg xids root str = |
305 let |
306 let |
306 val {lexicon, gram, parse_ast_trtab, ...} = tabs; |
307 val {lexicon, gram, parse_ast_trtab, roots, ...} = tabs; |
307 val pts = parse gram root (tokenize lexicon xids str); |
308 val root' = if root mem (roots \\ ["type", "prop"]) then "@logic_H" |
308 |
309 else if root = "prop" then "@prop_H" else root; |
309 fun show_pt pt = |
310 val pts = parse gram root' (tokenize lexicon xids str); |
310 writeln (str_of_ast (pt_to_ast (K None) pt)); |
311 |
311 in |
312 fun show_pt pt = writeln (str_of_ast (pt_to_ast (K None) pt)); |
312 (case pts of |
313 in |
313 [pt] => pt_to_ast (lookup_trtab parse_ast_trtab) pt |
314 if print_msg andalso length pts > 1 then |
314 | _ => |
315 (writeln ("Warning: Ambiguous input " ^ quote str); |
315 (writeln ("Ambiguous input " ^ quote str); |
316 writeln "produces the following parse trees:"; seq show_pt pts) |
316 writeln "produces the following parse trees:"; seq show_pt pts; |
317 else (); |
317 error "Please disambiguate the grammar or your input.")) |
318 map (pt_to_ast (lookup_trtab parse_ast_trtab)) pts |
318 end; |
319 end; |
319 |
320 |
320 |
321 |
321 (* read *) |
322 (* read *) |
322 |
323 |
323 fun read (syn as Syntax tabs) ty str = |
324 fun read (syn as Syntax tabs) ty str = |
324 let |
325 let |
325 val {parse_ruletab, parse_trtab, ...} = tabs; |
326 val {parse_ruletab, parse_trtab, ...} = tabs; |
326 val ast = read_ast syn false (typ_to_nonterm ty) str; |
327 val asts = read_asts syn true false (typ_to_nonterm ty) str; |
327 in |
328 in |
328 ast_to_term (lookup_trtab parse_trtab) |
329 map (ast_to_term (lookup_trtab parse_trtab)) |
329 (normalize_ast (lookup_ruletab parse_ruletab) ast) |
330 (map (normalize_ast (lookup_ruletab parse_ruletab)) asts) |
330 end; |
331 end; |
331 |
332 |
332 |
333 |
333 (* read types *) |
334 (* read types *) |
334 |
335 |
335 fun read_typ syn def_sort str = |
336 fun read_typ syn def_sort str = |
336 let |
337 let |
337 val t = read syn typeT str; |
338 val ts = read syn typeT str; |
|
339 val t = case ts of |
|
340 [t'] => t' |
|
341 | _ => error "This should not happen while parsing a type.\n\ |
|
342 \Please check your type syntax for ambiguities!"; |
338 val sort_env = raw_term_sorts t; |
343 val sort_env = raw_term_sorts t; |
339 in |
344 in |
340 typ_of_term sort_env def_sort t |
345 typ_of_term sort_env def_sort t |
341 end; |
346 end; |
342 |
347 |
343 fun simple_read_typ str = read_typ type_syn (K []) str; |
348 fun simple_read_typ str = read_typ type_syn (K []) str; |
344 |
349 |
345 |
350 |
346 (* read rules *) |
351 (* read rules *) |
347 |
352 |
348 fun read_rule syn (xrule as ((_, lhs_src), (_, rhs_src))) = |
353 fun read_rule (syn as Syntax tabs) print_msg |
|
354 (check_types: bool -> term list * typ -> int * term * 'a) |
|
355 (xrule as ((_, lhs_src), (_, rhs_src))) = |
349 let |
356 let |
350 val Syntax {consts, ...} = syn; |
357 val Syntax {consts, ...} = syn; |
351 |
358 |
352 fun constantify (ast as Constant _) = ast |
359 fun constantify (ast as Constant _) = ast |
353 | constantify (ast as Variable x) = |
360 | constantify (ast as Variable x) = |
354 if x mem consts then Constant x else ast |
361 if x mem consts then Constant x else ast |
355 | constantify (Appl asts) = Appl (map constantify asts); |
362 | constantify (Appl asts) = Appl (map constantify asts); |
356 |
363 |
357 fun read_pat (root, str) = |
364 fun read_pat (root, str) = |
358 constantify (read_ast syn true root str) |
365 let val {parse_ruletab, parse_trtab, ...} = tabs; |
359 handle ERROR => error ("The error above occurred in " ^ quote str); |
366 val asts = read_asts syn print_msg true root str; |
|
367 val ts = map (ast_to_term (lookup_trtab parse_trtab)) |
|
368 (map (normalize_ast (lookup_ruletab parse_ruletab)) asts); |
|
369 |
|
370 val idx = if length ts = 1 then 0 |
|
371 else (if print_msg then |
|
372 writeln ("This occured in syntax translation rule: " ^ |
|
373 quote lhs_src ^ " -> " ^ quote rhs_src) |
|
374 else (); |
|
375 #1 (check_types print_msg (ts, Type (root, [])))) |
|
376 in constantify (nth_elem (idx, asts)) |
|
377 handle ERROR => error ("The error above occurred in " ^ quote str) |
|
378 end; |
360 |
379 |
361 val rule as (lhs, rhs) = (pairself read_pat) xrule; |
380 val rule as (lhs, rhs) = (pairself read_pat) xrule; |
362 in |
381 in |
363 (case rule_error rule of |
382 (case rule_error rule of |
364 Some msg => |
383 Some msg => |
372 datatype xrule = |
391 datatype xrule = |
373 op |-> of (string * string) * (string * string) | |
392 op |-> of (string * string) * (string * string) | |
374 op <-| of (string * string) * (string * string) | |
393 op <-| of (string * string) * (string * string) | |
375 op <-> of (string * string) * (string * string); |
394 op <-> of (string * string) * (string * string); |
376 |
395 |
377 fun read_xrules syn xrules = |
396 fun read_xrules syn check_types xrules = |
378 let |
397 let |
379 fun right_rule (xpat1 |-> xpat2) = Some (xpat1, xpat2) |
398 fun right_rule (xpat1 |-> xpat2) = Some (xpat1, xpat2) |
380 | right_rule (xpat1 <-| xpat2) = None |
399 | right_rule (xpat1 <-| xpat2) = None |
381 | right_rule (xpat1 <-> xpat2) = Some (xpat1, xpat2); |
400 | right_rule (xpat1 <-> xpat2) = Some (xpat1, xpat2); |
382 |
401 |
383 fun left_rule (xpat1 |-> xpat2) = None |
402 fun left_rule (xpat1 |-> xpat2) = None |
384 | left_rule (xpat1 <-| xpat2) = Some (xpat2, xpat1) |
403 | left_rule (xpat1 <-| xpat2) = Some (xpat2, xpat1) |
385 | left_rule (xpat1 <-> xpat2) = Some (xpat2, xpat1); |
404 | left_rule (xpat1 <-> xpat2) = Some (xpat2, xpat1); |
386 in |
405 |
387 (map (read_rule syn) (mapfilter right_rule xrules), |
406 val rrules = mapfilter right_rule xrules; |
388 map (read_rule syn) (mapfilter left_rule xrules)) |
407 val lrules = mapfilter left_rule xrules; |
|
408 in |
|
409 (map (read_rule syn true check_types) rrules, |
|
410 map (read_rule syn (rrules = []) check_types) lrules) |
389 end; |
411 end; |
390 |
412 |
391 |
413 |
392 |
414 |
393 (** pretty terms or typs **) |
415 (** pretty terms or typs **) |