68 val kill: transition -> transition |
68 val kill: transition -> transition |
69 val history: (node History.T -> node History.T) -> transition -> transition |
69 val history: (node History.T -> node History.T) -> transition -> transition |
70 val keep: (state -> unit) -> transition -> transition |
70 val keep: (state -> unit) -> transition -> transition |
71 val keep': (bool -> state -> unit) -> transition -> transition |
71 val keep': (bool -> state -> unit) -> transition -> transition |
72 val imperative: (unit -> unit) -> transition -> transition |
72 val imperative: (unit -> unit) -> transition -> transition |
73 val init_theory: (bool -> theory) -> (theory -> unit) -> (theory -> unit) |
73 val init_theory: (bool -> theory) -> (theory -> unit) -> (theory -> unit) -> |
74 -> transition -> transition |
74 transition -> transition |
75 val theory: (theory -> theory) -> transition -> transition |
75 val theory: (theory -> theory) -> transition -> transition |
76 val theory': (bool -> theory -> theory) -> transition -> transition |
76 val theory': (bool -> theory -> theory) -> transition -> transition |
77 val exit_local_theory: transition -> transition |
|
78 val begin_local_theory: bool -> (theory -> local_theory) -> transition -> transition |
77 val begin_local_theory: bool -> (theory -> local_theory) -> transition -> transition |
|
78 val end_local_theory: transition -> transition |
79 val local_theory: xstring option -> (local_theory -> local_theory) -> transition -> transition |
79 val local_theory: xstring option -> (local_theory -> local_theory) -> transition -> transition |
80 val present_local_theory: xstring option -> (bool -> node -> unit) -> transition -> transition |
80 val present_local_theory: xstring option -> (bool -> node -> unit) -> transition -> transition |
|
81 val local_theory_to_proof: xstring option -> (local_theory -> Proof.state) -> |
|
82 transition -> transition |
81 val theory_to_proof: (theory -> Proof.state) -> transition -> transition |
83 val theory_to_proof: (theory -> Proof.state) -> transition -> transition |
|
84 val end_proof: (bool -> Proof.state -> Proof.context) -> transition -> transition |
|
85 val forget_proof: transition -> transition |
82 val proof: (Proof.state -> Proof.state) -> transition -> transition |
86 val proof: (Proof.state -> Proof.state) -> transition -> transition |
83 val proofs: (Proof.state -> Proof.state Seq.seq) -> transition -> transition |
87 val proofs: (Proof.state -> Proof.state Seq.seq) -> transition -> transition |
84 val proof': (bool -> Proof.state -> Proof.state) -> transition -> transition |
88 val proof': (bool -> Proof.state -> Proof.state) -> transition -> transition |
85 val proofs': (bool -> Proof.state -> Proof.state Seq.seq) -> transition -> transition |
89 val proofs': (bool -> Proof.state -> Proof.state Seq.seq) -> transition -> transition |
|
90 val present_proof: (bool -> node -> unit) -> transition -> transition |
86 val actual_proof: (ProofHistory.T -> ProofHistory.T) -> transition -> transition |
91 val actual_proof: (ProofHistory.T -> ProofHistory.T) -> transition -> transition |
87 val skip_proof: (int History.T -> int History.T) -> transition -> transition |
92 val skip_proof: (int History.T -> int History.T) -> transition -> transition |
88 val skip_proof_to_theory: (int -> bool) -> transition -> transition |
93 val skip_proof_to_theory: (int -> bool) -> transition -> transition |
89 val forget_proof: transition -> transition |
|
90 val end_proof: (bool -> Proof.state -> Proof.context) -> transition -> transition |
|
91 val present_proof: (bool -> node -> unit) -> transition -> transition |
|
92 val unknown_theory: transition -> transition |
94 val unknown_theory: transition -> transition |
93 val unknown_proof: transition -> transition |
95 val unknown_proof: transition -> transition |
94 val unknown_context: transition -> transition |
96 val unknown_context: transition -> transition |
95 val apply: bool -> transition -> state -> (state * (exn * string) option) option |
97 val apply: bool -> transition -> state -> (state * (exn * string) option) option |
96 val present_excursion: (transition * (state -> state -> 'a -> 'a)) list -> 'a -> 'a |
98 val present_excursion: (transition * (state -> state -> 'a -> 'a)) list -> 'a -> 'a |
116 |
118 |
117 type generic_theory = Context.generic; (*theory or local_theory*) |
119 type generic_theory = Context.generic; (*theory or local_theory*) |
118 |
120 |
119 datatype node = |
121 datatype node = |
120 Theory of generic_theory * Proof.context option | (*theory with presentation context*) |
122 Theory of generic_theory * Proof.context option | (*theory with presentation context*) |
121 Proof of ProofHistory.T * generic_theory | (*history of proof states, original theory*) |
123 Proof of ProofHistory.T * ((Proof.context -> generic_theory) * generic_theory) | |
122 SkipProof of (int History.T * generic_theory) * generic_theory; |
124 (*history of proof states, finish, original theory*) |
|
125 SkipProof of int History.T * (generic_theory * generic_theory); |
123 (*history of proof depths, resulting theory, original theory*) |
126 (*history of proof depths, resulting theory, original theory*) |
124 |
127 |
125 val theory_node = fn Theory (gthy, _) => SOME gthy | _ => NONE; |
128 val theory_node = fn Theory (gthy, _) => SOME gthy | _ => NONE; |
126 val proof_node = fn Proof (prf, _) => SOME prf | _ => NONE; |
129 val proof_node = fn Proof (prf, _) => SOME prf | _ => NONE; |
127 |
130 |
128 fun cases_node f _ (Theory (gthy, _)) = f gthy |
131 fun cases_node f _ (Theory (gthy, _)) = f gthy |
129 | cases_node _ g (Proof (prf, _)) = g (ProofHistory.current prf) |
132 | cases_node _ g (Proof (prf, _)) = g (ProofHistory.current prf) |
130 | cases_node f _ (SkipProof ((_, gthy), _)) = f gthy; |
133 | cases_node f _ (SkipProof (_, (gthy, _))) = f gthy; |
131 |
134 |
132 fun presentation_context (SOME (Theory (_, SOME ctxt))) NONE = ctxt |
135 fun presentation_context (SOME (Theory (_, SOME ctxt))) NONE = ctxt |
133 | presentation_context (SOME node) NONE = cases_node Context.proof_of Proof.context_of node |
136 | presentation_context (SOME node) NONE = cases_node Context.proof_of Proof.context_of node |
134 | presentation_context (SOME node) (SOME loc) = |
137 | presentation_context (SOME node) (SOME loc) = |
135 TheoryTarget.init (SOME loc) (cases_node Context.theory_of Proof.theory_of node) |
138 TheoryTarget.init (SOME loc) (cases_node Context.theory_of Proof.theory_of node) |
514 fun init_theory f exit kill = |
517 fun init_theory f exit kill = |
515 init (fn int => Theory (Context.Theory (f int), NONE)) |
518 init (fn int => Theory (Context.Theory (f int), NONE)) |
516 (fn Theory (Context.Theory thy, _) => exit thy | _ => raise UNDEF) |
519 (fn Theory (Context.Theory thy, _) => exit thy | _ => raise UNDEF) |
517 (fn Theory (Context.Theory thy, _) => kill thy | _ => raise UNDEF); |
520 (fn Theory (Context.Theory thy, _) => kill thy | _ => raise UNDEF); |
518 |
521 |
519 |
522 val unknown_theory = imperative (fn () => warning "Unknown theory context"); |
520 (* typed transitions *) |
523 val unknown_proof = imperative (fn () => warning "Unknown proof context"); |
|
524 val unknown_context = imperative (fn () => warning "Unknown context"); |
|
525 |
|
526 |
|
527 (* theory transitions *) |
521 |
528 |
522 fun theory' f = app_current (fn int => |
529 fun theory' f = app_current (fn int => |
523 (fn Theory (Context.Theory thy, _) => Theory (Context.Theory (f int thy), NONE) |
530 (fn Theory (Context.Theory thy, _) => Theory (Context.Theory (f int thy), NONE) |
524 | _ => raise UNDEF)); |
531 | _ => raise UNDEF)); |
525 |
532 |
526 fun theory f = theory' (K f); |
533 fun theory f = theory' (K f); |
527 |
|
528 val exit_local_theory = app_current (fn int => |
|
529 (fn Theory (Context.Proof lthy, _) => |
|
530 let val (ctxt', thy') = LocalTheory.exit int lthy |
|
531 in Theory (Context.Theory thy', SOME ctxt') end |
|
532 | _ => raise UNDEF)); |
|
533 |
534 |
534 fun begin_local_theory begin f = app_current (fn int => |
535 fun begin_local_theory begin f = app_current (fn int => |
535 (fn Theory (Context.Theory thy, _) => |
536 (fn Theory (Context.Theory thy, _) => |
536 let |
537 let |
537 val lthy = f thy; |
538 val lthy = f thy; |
538 val (ctxt, gthy) = |
539 val (ctxt, gthy) = |
539 if begin then (lthy, Context.Proof lthy) |
540 if begin then (lthy, Context.Proof lthy) |
540 else lthy |> LocalTheory.exit int ||> Context.Theory; |
541 else lthy |> LocalTheory.exit int ||> Context.Theory; |
541 in Theory (gthy, SOME ctxt) end |
542 in Theory (gthy, SOME ctxt) end |
542 | _ => raise UNDEF)); |
543 | _ => raise UNDEF)); |
|
544 |
|
545 val end_local_theory = app_current (fn int => |
|
546 (fn Theory (Context.Proof lthy, _) => |
|
547 let val (ctxt', thy') = LocalTheory.exit int lthy |
|
548 in Theory (Context.Theory thy', SOME ctxt') end |
|
549 | _ => raise UNDEF)); |
|
550 |
|
551 local |
543 |
552 |
544 fun local_theory_presentation loc f g = app_current (fn int => |
553 fun local_theory_presentation loc f g = app_current (fn int => |
545 (fn Theory (Context.Theory thy, _) => |
554 (fn Theory (Context.Theory thy, _) => |
546 let val (ctxt', thy') = TheoryTarget.mapping loc f thy |
555 let val (ctxt', thy') = TheoryTarget.mapping loc f thy |
547 in Theory (Context.Theory thy', SOME ctxt') end |
556 in Theory (Context.Theory thy', SOME ctxt') end |
550 if is_none loc then f lthy |> (fn lthy' => (lthy', LocalTheory.reinit lthy')) |
559 if is_none loc then f lthy |> (fn lthy' => (lthy', LocalTheory.reinit lthy')) |
551 else lthy |> LocalTheory.raw_theory_result (TheoryTarget.mapping loc f) |
560 else lthy |> LocalTheory.raw_theory_result (TheoryTarget.mapping loc f) |
552 in Theory (Context.Proof lthy', SOME ctxt') end |
561 in Theory (Context.Proof lthy', SOME ctxt') end |
553 | _ => raise UNDEF) #> tap (g int)); |
562 | _ => raise UNDEF) #> tap (g int)); |
554 |
563 |
|
564 in |
|
565 |
555 fun local_theory loc f = local_theory_presentation loc f (K I); |
566 fun local_theory loc f = local_theory_presentation loc f (K I); |
556 fun present_local_theory loc g = local_theory_presentation loc I g; |
567 fun present_local_theory loc g = local_theory_presentation loc I g; |
557 |
568 |
558 fun theory_to_proof f = app_current (fn int => |
569 end; |
559 (fn Theory (gthy as Context.Theory thy, _) => |
570 |
560 let |
571 |
561 val prf = f thy; |
572 (* proof transitions *) |
|
573 |
|
574 local |
|
575 |
|
576 fun begin_proof init finish = app_current (fn int => |
|
577 (fn Theory (gthy, _) => |
|
578 let |
|
579 val prf = init gthy; |
562 val schematic = Proof.schematic_goal prf; |
580 val schematic = Proof.schematic_goal prf; |
563 in |
581 in |
564 if ! skip_proofs andalso schematic then |
582 if ! skip_proofs andalso schematic then |
565 warning "Cannot skip proof of schematic goal statement" |
583 warning "Cannot skip proof of schematic goal statement" |
566 else (); |
584 else (); |
567 if ! skip_proofs andalso not schematic then |
585 if ! skip_proofs andalso not schematic then |
568 SkipProof ((History.init (undo_limit int) 0, |
586 SkipProof |
569 Context.Theory (ProofContext.theory_of (Proof.global_skip_proof int prf))), gthy) |
587 (History.init (undo_limit int) 0, (finish gthy (Proof.global_skip_proof int prf), gthy)) |
570 else Proof (ProofHistory.init (undo_limit int) prf, gthy) |
588 else Proof (ProofHistory.init (undo_limit int) prf, (finish gthy, gthy)) |
571 end |
589 end |
572 | _ => raise UNDEF)); |
590 | _ => raise UNDEF)); |
573 |
591 |
574 fun proofs' f = map_current (fn int => |
592 val global_finish = Context.Theory o ProofContext.theory_of; |
575 (fn Proof (prf, orig_gthy) => Proof (ProofHistory.applys (f int) prf, orig_gthy) |
593 val local_finish = Context.Proof o LocalTheory.reinit; |
576 | SkipProof ((h, gthy), orig_gthy) => SkipProof ((History.apply I h, gthy), orig_gthy) |
594 |
577 | _ => raise UNDEF)); |
595 fun mixed_finish (Context.Theory _) ctxt = global_finish ctxt |
578 |
596 | mixed_finish (Context.Proof lthy) ctxt = |
579 fun proof' f = proofs' (Seq.single oo f); |
597 Context.Proof (LocalTheory.raw_theory (K (ProofContext.theory_of ctxt)) lthy); |
580 val proofs = proofs' o K; |
598 |
581 val proof = proof' o K; |
599 in |
582 |
600 |
583 fun actual_proof f = map_current (fn _ => |
601 fun local_theory_to_proof NONE f = begin_proof |
584 (fn Proof (prf, orig_gthy) => Proof (f prf, orig_gthy) |
602 (fn Context.Theory thy => f (TheoryTarget.init NONE thy) |
585 | _ => raise UNDEF)); |
603 | Context.Proof lthy => f lthy) |
586 |
604 (fn Context.Theory _ => global_finish |
587 fun skip_proof f = map_current (fn _ => |
605 | Context.Proof _ => local_finish) |
588 (fn SkipProof ((h, gthy), orig_gthy) => SkipProof ((f h, gthy), orig_gthy) |
606 | local_theory_to_proof loc f = |
589 | _ => raise UNDEF)); |
607 begin_proof (f o TheoryTarget.init loc o Context.theory_of) mixed_finish; |
590 |
608 |
591 fun skip_proof_to_theory p = map_current (fn _ => |
609 fun theory_to_proof f = |
592 (fn SkipProof ((h, thy), _) => |
610 begin_proof (fn Context.Theory thy => f thy | _ => raise UNDEF) (K global_finish); |
593 if p (History.current h) then Theory (thy, NONE) |
611 |
594 else raise UNDEF |
612 end; |
595 | _ => raise UNDEF)); |
|
596 |
|
597 val forget_proof = map_current (fn _ => |
|
598 (fn Proof (_, orig_gthy) => Theory (orig_gthy, NONE) |
|
599 | SkipProof (_, orig_gthy) => Theory (orig_gthy, NONE) |
|
600 | _ => raise UNDEF)); |
|
601 |
613 |
602 fun end_proof f = map_current (fn int => |
614 fun end_proof f = map_current (fn int => |
603 (fn Proof (prf, orig_gthy) => |
615 (fn Proof (prf, (finish, orig_gthy)) => |
604 let val state = ProofHistory.current prf in |
616 let val state = ProofHistory.current prf in |
605 if can (Proof.assert_bottom true) state then |
617 if can (Proof.assert_bottom true) state then |
606 let |
618 let |
607 val ctxt' = f int state; |
619 val ctxt' = f int state; |
608 val gthy' = |
620 val gthy' = finish ctxt'; |
609 if can Context.the_theory orig_gthy |
|
610 then Context.Theory (ProofContext.theory_of ctxt') |
|
611 else Context.Proof (LocalTheory.reinit ctxt'); |
|
612 in Theory (gthy', SOME ctxt') end |
621 in Theory (gthy', SOME ctxt') end |
613 else raise UNDEF |
622 else raise UNDEF |
614 end |
623 end |
615 | SkipProof ((h, gthy), _) => |
624 | SkipProof (h, (gthy, _)) => |
616 if History.current h = 0 then Theory (gthy, NONE) else raise UNDEF |
625 if History.current h = 0 then Theory (gthy, NONE) else raise UNDEF |
617 | _ => raise UNDEF)); |
626 | _ => raise UNDEF)); |
618 |
627 |
|
628 val forget_proof = map_current (fn _ => |
|
629 (fn Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE) |
|
630 | SkipProof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE) |
|
631 | _ => raise UNDEF)); |
|
632 |
|
633 fun proofs' f = map_current (fn int => |
|
634 (fn Proof (prf, x) => Proof (ProofHistory.applys (f int) prf, x) |
|
635 | SkipProof (h, x) => SkipProof (History.apply I h, x) |
|
636 | _ => raise UNDEF)); |
|
637 |
|
638 fun proof' f = proofs' (Seq.single oo f); |
|
639 val proofs = proofs' o K; |
|
640 val proof = proof' o K; |
|
641 |
619 fun present_proof f = map_current (fn int => |
642 fun present_proof f = map_current (fn int => |
620 (fn node as Proof _ => node | _ => raise UNDEF) #> tap (f int)); |
643 (fn node as Proof _ => node |
621 |
644 | node as SkipProof _ => node |
622 val unknown_theory = imperative (fn () => warning "Unknown theory context"); |
645 | _ => raise UNDEF) #> tap (f int)); |
623 val unknown_proof = imperative (fn () => warning "Unknown proof context"); |
646 |
624 val unknown_context = imperative (fn () => warning "Unknown context"); |
647 fun actual_proof f = map_current (fn _ => |
|
648 (fn Proof (prf, x) => Proof (f prf, x) |
|
649 | _ => raise UNDEF)); |
|
650 |
|
651 fun skip_proof f = map_current (fn _ => |
|
652 (fn SkipProof (h, x) => SkipProof (f h, x) |
|
653 | _ => raise UNDEF)); |
|
654 |
|
655 fun skip_proof_to_theory p = map_current (fn _ => |
|
656 (fn SkipProof (h, (gthy, _)) => |
|
657 if p (History.current h) then Theory (gthy, NONE) |
|
658 else raise UNDEF |
|
659 | _ => raise UNDEF)); |
625 |
660 |
626 |
661 |
627 |
662 |
628 (** toplevel transactions **) |
663 (** toplevel transactions **) |
629 |
664 |