416 in ctxt |> Context_Position.restore_visible ctxt0 |> Thm.restore_hyps ctxt0 end); |
416 in ctxt |> Context_Position.restore_visible ctxt0 |> Thm.restore_hyps ctxt0 end); |
417 |
417 |
418 |
418 |
419 (* Potentially lazy notes *) |
419 (* Potentially lazy notes *) |
420 |
420 |
|
421 fun make_notes kind = map (fn ((b, atts), facts) => |
|
422 if null atts andalso forall (null o #2) facts |
|
423 then Lazy_Notes (kind, (b, Lazy.value (maps #1 facts))) |
|
424 else Notes (kind, [((b, atts), facts)])); |
|
425 |
421 fun lazy_notes thy loc = |
426 fun lazy_notes thy loc = |
422 rev (#notes (the_locale thy loc)) |> maps (fn ((kind, notes), _) => |
427 rev (#notes (the_locale thy loc)) |
423 notes |> map (fn ((b, atts), facts) => |
428 |> maps (fn ((kind, notes), _) => make_notes kind notes); |
424 if null atts andalso forall (null o #2) facts andalso false (* FIXME *) |
|
425 then Lazy_Notes (kind, (b, Lazy.value (maps #1 facts))) |
|
426 else Notes (kind, [((b, atts), facts)]))); |
|
427 |
429 |
428 fun consolidate_notes elems = |
430 fun consolidate_notes elems = |
429 (elems |
431 elems |
430 |> map_filter (fn Lazy_Notes (_, (_, ths)) => SOME ths | _ => NONE) |
432 |> map_filter (fn Lazy_Notes (_, (_, ths)) => SOME ths | _ => NONE) |
431 |> Lazy.consolidate; |
433 |> Lazy.consolidate |
432 elems); |
434 |> ignore; |
433 |
435 |
434 fun force_notes (Lazy_Notes (kind, (b, ths))) = Notes (kind, [((b, []), [(Lazy.force ths, [])])]) |
436 fun force_notes (Lazy_Notes (kind, (b, ths))) = Notes (kind, [((b, []), [(Lazy.force ths, [])])]) |
435 | force_notes elem = elem; |
437 | force_notes elem = elem; |
436 |
438 |
437 |
439 |
609 |
611 |
610 fun add_facts loc kind facts ctxt = |
612 fun add_facts loc kind facts ctxt = |
611 if null facts then ctxt |
613 if null facts then ctxt |
612 else |
614 else |
613 let |
615 let |
614 val notes = ((kind, map Attrib.trim_context_fact facts), serial ()); |
616 val stored_notes = ((kind, map Attrib.trim_context_fact facts), serial ()); |
615 |
617 val applied_notes = make_notes kind facts; |
616 fun global_notes morph thy = thy |
618 |
617 |> Attrib.global_notes kind |
619 fun apply_notes morph = applied_notes |> fold (fn elem => fn context => |
618 (Attrib.transform_facts (Morphism.transfer_morphism thy $> morph) facts) |> #2; |
620 let val elem' = Element.transform_ctxt (Morphism.transfer_morphism'' context $> morph) elem |
619 fun apply_registrations thy = |
621 in Element.init elem' context end); |
620 fold_rev (global_notes o #2) (registrations_of (Context.Theory thy) loc) thy; |
622 val apply_registrations = Context.theory_map (fn context => |
|
623 fold_rev (apply_notes o #2) (registrations_of context loc) context); |
621 in |
624 in |
622 ctxt |
625 ctxt |
623 |> Attrib.local_notes kind facts |> #2 |
626 |> Attrib.local_notes kind facts |> #2 |
624 |> Proof_Context.background_theory |
627 |> Proof_Context.background_theory |
625 ((change_locale loc o apfst o apsnd) (cons notes) #> apply_registrations) |
628 ((change_locale loc o apfst o apsnd) (cons stored_notes) #> apply_registrations) |
626 end; |
629 end; |
627 |
630 |
628 fun add_declaration loc syntax decl = |
631 fun add_declaration loc syntax decl = |
629 syntax ? |
632 syntax ? |
630 Proof_Context.background_theory ((change_locale loc o apfst o apfst) (cons (decl, serial ()))) |
633 Proof_Context.background_theory ((change_locale loc o apfst o apfst) (cons (decl, serial ()))) |
701 | cons_elem (elem as Lazy_Notes _) = show_facts ? cons elem |
704 | cons_elem (elem as Lazy_Notes _) = show_facts ? cons elem |
702 | cons_elem elem = cons elem; |
705 | cons_elem elem = cons elem; |
703 val elems = |
706 val elems = |
704 activate_all name thy cons_elem (K (Morphism.transfer_morphism thy)) (empty_idents, []) |
707 activate_all name thy cons_elem (K (Morphism.transfer_morphism thy)) (empty_idents, []) |
705 |> snd |> rev |
708 |> snd |> rev |
706 |> consolidate_notes |
709 |> tap consolidate_notes |
707 |> map force_notes; |
710 |> map force_notes; |
708 in |
711 in |
709 Pretty.keyword1 "locale" :: Pretty.brk 1 :: pretty_name locale_ctxt name :: |
712 Pretty.keyword1 "locale" :: Pretty.brk 1 :: pretty_name locale_ctxt name :: |
710 maps (fn elem => [Pretty.fbrk, Pretty.chunks (Element.pretty_ctxt locale_ctxt elem)]) elems |
713 maps (fn elem => [Pretty.fbrk, Pretty.chunks (Element.pretty_ctxt locale_ctxt elem)]) elems |
711 end; |
714 end; |