110 (** toplevel state **) |
110 (** toplevel state **) |
111 |
111 |
112 exception UNDEF; |
112 exception UNDEF; |
113 |
113 |
114 |
114 |
|
115 (* local theory wrappers *) |
|
116 |
|
117 type generic_theory = Context.generic; (*theory or local_theory*) |
|
118 |
|
119 val loc_init = TheoryTarget.init; |
|
120 |
|
121 val loc_exit = ProofContext.theory_of o LocalTheory.exit; |
|
122 |
|
123 fun loc_begin loc (Context.Theory thy) = loc_init loc thy |
|
124 | loc_begin NONE (Context.Proof lthy) = lthy |
|
125 | loc_begin loc (Context.Proof lthy) = loc_init loc (loc_exit lthy); |
|
126 |
|
127 fun loc_finish _ (Context.Theory _) = Context.Theory o loc_exit |
|
128 | loc_finish NONE (Context.Proof _) = Context.Proof o LocalTheory.restore |
|
129 | loc_finish (SOME _) (Context.Proof lthy) = Context.Proof o LocalTheory.reinit lthy o loc_exit; |
|
130 |
|
131 |
115 (* datatype state *) |
132 (* datatype state *) |
116 |
|
117 type generic_theory = Context.generic; (*theory or local_theory*) |
|
118 |
133 |
119 datatype node = |
134 datatype node = |
120 Theory of generic_theory * Proof.context option | (*theory with presentation context*) |
135 Theory of generic_theory * Proof.context option | (*theory with presentation context*) |
121 Proof of ProofHistory.T * ((Proof.context -> generic_theory) * generic_theory) | |
136 Proof of ProofHistory.T * ((Proof.context -> generic_theory) * generic_theory) | |
122 (*history of proof states, finish, original theory*) |
137 (*history of proof states, finish, original theory*) |
131 | cases_node f _ (SkipProof (_, (gthy, _))) = f gthy; |
146 | cases_node f _ (SkipProof (_, (gthy, _))) = f gthy; |
132 |
147 |
133 fun presentation_context (SOME (Theory (_, SOME ctxt))) NONE = ctxt |
148 fun presentation_context (SOME (Theory (_, SOME ctxt))) NONE = ctxt |
134 | presentation_context (SOME node) NONE = cases_node Context.proof_of Proof.context_of node |
149 | presentation_context (SOME node) NONE = cases_node Context.proof_of Proof.context_of node |
135 | presentation_context (SOME node) (SOME loc) = |
150 | presentation_context (SOME node) (SOME loc) = |
136 TheoryTarget.init (SOME loc) (cases_node Context.theory_of Proof.theory_of node) |
151 loc_init (SOME loc) (cases_node Context.theory_of Proof.theory_of node) |
137 | presentation_context NONE _ = raise UNDEF; |
152 | presentation_context NONE _ = raise UNDEF; |
138 |
153 |
139 datatype state = State of (node History.T * ((node -> unit) * (node -> unit))) option; |
154 datatype state = State of (node History.T * ((node -> unit) * (node -> unit))) option; |
140 |
155 |
141 val toplevel = State NONE; |
156 val toplevel = State NONE; |
518 (fn Theory (Context.Theory thy, _) => Theory (Context.Theory (f int thy), NONE) |
533 (fn Theory (Context.Theory thy, _) => Theory (Context.Theory (f int thy), NONE) |
519 | _ => raise UNDEF)); |
534 | _ => raise UNDEF)); |
520 |
535 |
521 fun theory f = theory' (K f); |
536 fun theory f = theory' (K f); |
522 |
537 |
523 fun begin_local_theory begin f = app_current (fn int => |
538 fun begin_local_theory begin f = app_current (fn _ => |
524 (fn Theory (Context.Theory thy, _) => |
539 (fn Theory (Context.Theory thy, _) => |
525 let |
540 let |
526 val lthy = f thy; |
541 val lthy = f thy; |
527 val (ctxt, gthy) = |
542 val gthy = if begin then Context.Proof lthy else Context.Theory (loc_exit lthy); |
528 if begin then (lthy, Context.Proof lthy) |
543 in Theory (gthy, SOME lthy) end |
529 else lthy |> LocalTheory.exit int ||> Context.Theory; |
|
530 in Theory (gthy, SOME ctxt) end |
|
531 | _ => raise UNDEF)); |
544 | _ => raise UNDEF)); |
532 |
545 |
533 val end_local_theory = app_current (fn int => |
546 val end_local_theory = app_current (fn _ => |
534 (fn Theory (Context.Proof lthy, _) => |
547 (fn Theory (Context.Proof lthy, _) => Theory (Context.Theory (loc_exit lthy), SOME lthy) |
535 let val (ctxt', thy') = LocalTheory.exit int lthy |
|
536 in Theory (Context.Theory thy', SOME ctxt') end |
|
537 | _ => raise UNDEF)); |
548 | _ => raise UNDEF)); |
538 |
549 |
539 local |
550 local |
540 |
551 |
541 fun local_theory_presentation loc f g = app_current (fn int => |
552 fun local_theory_presentation loc f g = app_current (fn int => |
542 (fn Theory (Context.Theory thy, _) => |
553 (fn Theory (gthy, _) => |
543 let val (ctxt', thy') = TheoryTarget.mapping loc f thy |
554 let |
544 in Theory (Context.Theory thy', SOME ctxt') end |
555 val finish = loc_finish loc gthy; |
545 | Theory (Context.Proof lthy, _) => |
556 val lthy' = f (loc_begin loc gthy); |
546 let val (ctxt', lthy') = |
557 in Theory (finish lthy', SOME lthy') end |
547 if is_none loc then f lthy |> (fn lthy' => (lthy', LocalTheory.restore lthy')) |
|
548 else lthy |> LocalTheory.raw_theory_result (TheoryTarget.mapping loc f) |
|
549 in Theory (Context.Proof lthy', SOME ctxt') end |
|
550 | _ => raise UNDEF) #> tap (g int)); |
558 | _ => raise UNDEF) #> tap (g int)); |
551 |
559 |
552 in |
560 in |
553 |
561 |
554 fun local_theory loc f = local_theory_presentation loc f (K I); |
562 fun local_theory loc f = local_theory_presentation loc f (K I); |
556 |
564 |
557 end; |
565 end; |
558 |
566 |
559 |
567 |
560 (* proof transitions *) |
568 (* proof transitions *) |
561 |
|
562 local |
|
563 |
|
564 fun begin_proof init finish = app_current (fn int => |
|
565 (fn Theory (gthy, _) => |
|
566 let |
|
567 val prf = init gthy; |
|
568 val schematic = Proof.schematic_goal prf; |
|
569 in |
|
570 if ! skip_proofs andalso schematic then |
|
571 warning "Cannot skip proof of schematic goal statement" |
|
572 else (); |
|
573 if ! skip_proofs andalso not schematic then |
|
574 SkipProof |
|
575 (History.init (undo_limit int) 0, (finish gthy (Proof.global_skip_proof int prf), gthy)) |
|
576 else Proof (ProofHistory.init (undo_limit int) prf, (finish gthy, gthy)) |
|
577 end |
|
578 | _ => raise UNDEF)); |
|
579 |
|
580 val global_finish = Context.Theory o ProofContext.theory_of; |
|
581 val local_finish = Context.Proof o LocalTheory.restore; |
|
582 |
|
583 fun mixed_finish (Context.Theory _) ctxt = global_finish ctxt |
|
584 | mixed_finish (Context.Proof lthy) ctxt = |
|
585 Context.Proof (LocalTheory.raw_theory (K (ProofContext.theory_of ctxt)) lthy); |
|
586 |
|
587 in |
|
588 |
|
589 fun local_theory_to_proof NONE f = begin_proof |
|
590 (fn Context.Theory thy => f (TheoryTarget.init NONE thy) |
|
591 | Context.Proof lthy => f lthy) |
|
592 (fn Context.Theory _ => global_finish |
|
593 | Context.Proof _ => local_finish) |
|
594 | local_theory_to_proof loc f = |
|
595 begin_proof (f o TheoryTarget.init loc o Context.theory_of) mixed_finish; |
|
596 |
|
597 fun theory_to_proof f = |
|
598 begin_proof (fn Context.Theory thy => f thy | _ => raise UNDEF) (K global_finish); |
|
599 |
|
600 end; |
|
601 |
569 |
602 fun end_proof f = map_current (fn int => |
570 fun end_proof f = map_current (fn int => |
603 (fn Proof (prf, (finish, orig_gthy)) => |
571 (fn Proof (prf, (finish, orig_gthy)) => |
604 let val state = ProofHistory.current prf in |
572 let val state = ProofHistory.current prf in |
605 if can (Proof.assert_bottom true) state then |
573 if can (Proof.assert_bottom true) state then |
611 end |
579 end |
612 | SkipProof (h, (gthy, _)) => |
580 | SkipProof (h, (gthy, _)) => |
613 if History.current h = 0 then Theory (gthy, NONE) else raise UNDEF |
581 if History.current h = 0 then Theory (gthy, NONE) else raise UNDEF |
614 | _ => raise UNDEF)); |
582 | _ => raise UNDEF)); |
615 |
583 |
|
584 local |
|
585 |
|
586 fun begin_proof init finish = app_current (fn int => |
|
587 (fn Theory (gthy, _) => |
|
588 let |
|
589 val prf = init gthy; |
|
590 val schematic = Proof.schematic_goal prf; |
|
591 in |
|
592 if ! skip_proofs andalso schematic then |
|
593 warning "Cannot skip proof of schematic goal statement" |
|
594 else (); |
|
595 if ! skip_proofs andalso not schematic then |
|
596 SkipProof |
|
597 (History.init (undo_limit int) 0, (finish gthy (Proof.global_skip_proof int prf), gthy)) |
|
598 else Proof (ProofHistory.init (undo_limit int) prf, (finish gthy, gthy)) |
|
599 end |
|
600 | _ => raise UNDEF)); |
|
601 |
|
602 in |
|
603 |
|
604 fun local_theory_to_proof loc f = begin_proof (f o loc_begin loc) (loc_finish loc); |
|
605 |
|
606 fun theory_to_proof f = begin_proof |
|
607 (fn Context.Theory thy => f thy | _ => raise UNDEF) |
|
608 (K (Context.Theory o ProofContext.theory_of)); |
|
609 |
|
610 end; |
|
611 |
616 val forget_proof = map_current (fn _ => |
612 val forget_proof = map_current (fn _ => |
617 (fn Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE) |
613 (fn Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE) |
618 | SkipProof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE) |
614 | SkipProof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE) |
619 | _ => raise UNDEF)); |
615 | _ => raise UNDEF)); |
620 |
616 |