419 |
417 |
420 fun parse_ast_pattern ctxt (root, str) = |
418 fun parse_ast_pattern ctxt (root, str) = |
421 let |
419 let |
422 val syn = Proof_Context.syn_of ctxt; |
420 val syn = Proof_Context.syn_of ctxt; |
423 |
421 |
424 fun constify (ast as Ast.Constant _) = ast |
422 val reports = Unsynchronized.ref ([]: Position.report list); |
425 | constify (ast as Ast.Variable x) = |
423 fun report ps = Position.store_reports reports ps; |
|
424 |
|
425 fun decode_const ps c = (report ps (markup_entity ctxt) c; Ast.Constant c); |
|
426 fun decode_var ps x = (report ps (fn () => [Markup.name x Markup.free]) (); Ast.Variable x); |
|
427 fun decode_appl ps asts = Ast.Appl (map (decode ps) asts) |
|
428 and decode ps (Ast.Constant c) = decode_const ps c |
|
429 | decode ps (Ast.Variable x) = |
426 if is_some (Syntax.lookup_const syn x) orelse Long_Name.is_qualified x |
430 if is_some (Syntax.lookup_const syn x) orelse Long_Name.is_qualified x |
427 then Ast.Constant x |
431 then decode_const ps x |
428 else ast |
432 else decode_var ps x |
429 | constify (Ast.Appl asts) = Ast.Appl (map constify asts); |
433 | decode ps (Ast.Appl (asts as (Ast.Constant c :: ast :: Ast.Variable x :: args))) = |
|
434 if member (op =) Term_Position.markers c then |
|
435 (case Term_Position.decode x of |
|
436 SOME p => Ast.mk_appl (decode (p :: ps) ast) (map (decode ps) args) |
|
437 | NONE => decode_appl ps asts) |
|
438 else decode_appl ps asts |
|
439 | decode ps (Ast.Appl asts) = decode_appl ps asts; |
430 |
440 |
431 val (syms, pos) = Syntax.read_token str; |
441 val (syms, pos) = Syntax.read_token str; |
432 in |
442 val ast = |
433 parse_asts ctxt true root (syms, pos) |
443 parse_asts ctxt true root (syms, pos) |
434 |> uncurry (report_result ctxt pos) |
444 |> uncurry (report_result ctxt pos) |
435 |> constify |
445 |> decode []; |
436 end; |
446 val _ = Context_Position.reports ctxt (! reports); |
|
447 in ast end; |
437 |
448 |
438 |
449 |
439 |
450 |
440 (** encode parse trees **) |
451 (** encode parse trees **) |
441 |
452 |
539 | mark Ts (t as t1 $ t2) = |
550 | mark Ts (t as t1 $ t2) = |
540 (if is_Const (Term.head_of t) orelse not (is_prop Ts t) then I else aprop) |
551 (if is_Const (Term.head_of t) orelse not (is_prop Ts t) then I else aprop) |
541 (mark Ts t1 $ mark Ts t2); |
552 (mark Ts t1 $ mark Ts t2); |
542 in mark [] tm end; |
553 in mark [] tm end; |
543 |
554 |
544 in |
555 fun prune_types ctxt tm = |
545 |
556 let |
546 fun term_to_ast idents is_syntax_const ctxt trf tm = |
|
547 let |
|
548 val show_types = |
|
549 Config.get ctxt show_types orelse Config.get ctxt show_sorts orelse |
|
550 Config.get ctxt show_all_types; |
|
551 val show_structs = Config.get ctxt show_structs; |
|
552 val show_free_types = Config.get ctxt show_free_types; |
557 val show_free_types = Config.get ctxt show_free_types; |
553 val show_all_types = Config.get ctxt show_all_types; |
558 |
554 val show_markup = Config.get ctxt show_markup; |
559 fun prune (t_seen as (Const _, _)) = t_seen |
555 |
560 | prune (t as Free (x, ty), seen) = |
556 val {structs, fixes} = idents; |
|
557 |
|
558 fun prune_typs (t_seen as (Const _, _)) = t_seen |
|
559 | prune_typs (t as Free (x, ty), seen) = |
|
560 if ty = dummyT then (t, seen) |
561 if ty = dummyT then (t, seen) |
561 else if not show_free_types orelse member (op aconv) seen t then (Syntax.free x, seen) |
562 else if not show_free_types orelse member (op aconv) seen t then (Syntax.free x, seen) |
562 else (t, t :: seen) |
563 else (t, t :: seen) |
563 | prune_typs (t as Var (xi, ty), seen) = |
564 | prune (t as Var (xi, ty), seen) = |
564 if ty = dummyT then (t, seen) |
565 if ty = dummyT then (t, seen) |
565 else if not show_free_types orelse member (op aconv) seen t then (Syntax.var xi, seen) |
566 else if not show_free_types orelse member (op aconv) seen t then (Syntax.var xi, seen) |
566 else (t, t :: seen) |
567 else (t, t :: seen) |
567 | prune_typs (t_seen as (Bound _, _)) = t_seen |
568 | prune (t_seen as (Bound _, _)) = t_seen |
568 | prune_typs (Abs (x, ty, t), seen) = |
569 | prune (Abs (x, ty, t), seen) = |
569 let val (t', seen') = prune_typs (t, seen); |
570 let val (t', seen') = prune (t, seen); |
570 in (Abs (x, ty, t'), seen') end |
571 in (Abs (x, ty, t'), seen') end |
571 | prune_typs (t1 $ t2, seen) = |
572 | prune (t1 $ t2, seen) = |
572 let |
573 let |
573 val (t1', seen') = prune_typs (t1, seen); |
574 val (t1', seen') = prune (t1, seen); |
574 val (t2', seen'') = prune_typs (t2, seen'); |
575 val (t2', seen'') = prune (t2, seen'); |
575 in (t1' $ t2', seen'') end; |
576 in (t1' $ t2', seen'') end; |
576 |
577 in #1 (prune (tm, [])) end; |
577 fun mark_atoms ((t as Const (c, _)) $ u) = |
578 |
|
579 fun mark_atoms {structs, fixes} is_syntax_const ctxt tm = |
|
580 let |
|
581 val show_structs = Config.get ctxt show_structs; |
|
582 |
|
583 fun mark ((t as Const (c, _)) $ u) = |
578 if member (op =) Pure_Thy.token_markers c |
584 if member (op =) Pure_Thy.token_markers c |
579 then t $ u else mark_atoms t $ mark_atoms u |
585 then t $ u else mark t $ mark u |
580 | mark_atoms (t $ u) = mark_atoms t $ mark_atoms u |
586 | mark (t $ u) = mark t $ mark u |
581 | mark_atoms (Abs (x, T, t)) = Abs (x, T, mark_atoms t) |
587 | mark (Abs (x, T, t)) = Abs (x, T, mark t) |
582 | mark_atoms (t as Const (c, T)) = |
588 | mark (t as Const (c, T)) = |
583 if is_syntax_const c then t |
589 if is_syntax_const c then t |
584 else Const (Lexicon.mark_const c, T) |
590 else Const (Lexicon.mark_const c, T) |
585 | mark_atoms (t as Free (x, T)) = |
591 | mark (t as Free (x, T)) = |
586 let val i = find_index (fn s => s = x) structs + 1 in |
592 let val i = find_index (fn s => s = x) structs + 1 in |
587 if i = 0 andalso member (op =) fixes x then |
593 if i = 0 andalso member (op =) fixes x then |
588 Const (Lexicon.mark_fixed x, T) |
594 Const (Lexicon.mark_fixed x, T) |
589 else if i = 1 andalso not show_structs then |
595 else if i = 1 andalso not show_structs then |
590 Syntax.const "_struct" $ Syntax.const "_indexdefault" |
596 Syntax.const "_struct" $ Syntax.const "_indexdefault" |
591 else Syntax.const "_free" $ t |
597 else Syntax.const "_free" $ t |
592 end |
598 end |
593 | mark_atoms (t as Var (xi, T)) = |
599 | mark (t as Var (xi, T)) = |
594 if xi = Syntax_Ext.dddot_indexname then Const ("_DDDOT", T) |
600 if xi = Syntax_Ext.dddot_indexname then Const ("_DDDOT", T) |
595 else Syntax.const "_var" $ t |
601 else Syntax.const "_var" $ t |
596 | mark_atoms a = a; |
602 | mark a = a; |
|
603 in mark tm end; |
|
604 |
|
605 in |
|
606 |
|
607 fun term_to_ast idents is_syntax_const ctxt trf tm = |
|
608 let |
|
609 val show_types = Config.get ctxt show_types orelse Config.get ctxt show_sorts; |
|
610 val show_markup = Config.get ctxt show_markup; |
597 |
611 |
598 fun ast_of tm = |
612 fun ast_of tm = |
599 (case strip_comb tm of |
613 (case strip_comb tm of |
600 (t as Abs _, ts) => Ast.mk_appl (ast_of (Syntax_Trans.abs_tr' ctxt t)) (map ast_of ts) |
614 (t as Abs _, ts) => Ast.mk_appl (ast_of (Syntax_Trans.abs_tr' ctxt t)) (map ast_of ts) |
601 | ((c as Const ("_free", _)), Free (x, T) :: ts) => |
615 | ((c as Const ("_free", _)), Free (x, T) :: ts) => |