src/Pure/Isar/toplevel.ML
changeset 18563 1df7022eac6f
parent 17904 21c6894b5998
child 18564 2b8ac8bc9719
equal deleted inserted replaced
18562:15178f4aa203 18563:1df7022eac6f
     7 
     7 
     8 signature TOPLEVEL =
     8 signature TOPLEVEL =
     9 sig
     9 sig
    10   datatype node =
    10   datatype node =
    11     Theory of theory * Proof.context option |
    11     Theory of theory * Proof.context option |
    12     Proof of ProofHistory.T |
    12     Proof of ProofHistory.T * theory |
    13     SkipProof of int History.T * theory
    13     SkipProof of (int History.T * theory) * theory
    14   type state
    14   type state
    15   val toplevel: state
    15   val toplevel: state
    16   val is_toplevel: state -> bool
    16   val is_toplevel: state -> bool
    17   val level: state -> int
    17   val level: state -> int
    18   exception UNDEF
    18   exception UNDEF
    69     -> transition -> transition
    69     -> transition -> transition
    70   val theory: (theory -> theory) -> transition -> transition
    70   val theory: (theory -> theory) -> transition -> transition
    71   val theory': (bool -> theory -> theory) -> transition -> transition
    71   val theory': (bool -> theory -> theory) -> transition -> transition
    72   val theory_context: (theory -> theory * Proof.context) -> transition -> transition
    72   val theory_context: (theory -> theory * Proof.context) -> transition -> transition
    73   val theory_to_proof: (theory -> Proof.state) -> transition -> transition
    73   val theory_to_proof: (theory -> Proof.state) -> transition -> transition
       
    74   val theory_theory_to_proof: (theory -> Proof.state) -> transition -> transition
    74   val proof: (Proof.state -> Proof.state) -> transition -> transition
    75   val proof: (Proof.state -> Proof.state) -> transition -> transition
    75   val proofs: (Proof.state -> Proof.state Seq.seq) -> transition -> transition
    76   val proofs: (Proof.state -> Proof.state Seq.seq) -> transition -> transition
    76   val proof': (bool -> Proof.state -> Proof.state) -> transition -> transition
    77   val proof': (bool -> Proof.state -> Proof.state) -> transition -> transition
    77   val proofs': (bool -> Proof.state -> Proof.state Seq.seq) -> transition -> transition
    78   val proofs': (bool -> Proof.state -> Proof.state Seq.seq) -> transition -> transition
    78   val actual_proof: (ProofHistory.T -> ProofHistory.T) -> transition -> transition
    79   val actual_proof: (ProofHistory.T -> ProofHistory.T) -> transition -> transition
    80   val proof_to_theory: (Proof.state -> theory) -> transition -> transition
    81   val proof_to_theory: (Proof.state -> theory) -> transition -> transition
    81   val proof_to_theory': (bool -> Proof.state -> theory) -> transition -> transition
    82   val proof_to_theory': (bool -> Proof.state -> theory) -> transition -> transition
    82   val proof_to_theory_context: (bool -> Proof.state -> theory * Proof.context)
    83   val proof_to_theory_context: (bool -> Proof.state -> theory * Proof.context)
    83     -> transition -> transition
    84     -> transition -> transition
    84   val skip_proof_to_theory: (int -> bool) -> transition -> transition
    85   val skip_proof_to_theory: (int -> bool) -> transition -> transition
       
    86   val forget_proof: transition -> transition
    85   val unknown_theory: transition -> transition
    87   val unknown_theory: transition -> transition
    86   val unknown_proof: transition -> transition
    88   val unknown_proof: transition -> transition
    87   val unknown_context: transition -> transition
    89   val unknown_context: transition -> transition
    88   val exn_message: exn -> string
    90   val exn_message: exn -> string
    89   val apply: bool -> transition -> state -> (state * (exn * string) option) option
    91   val apply: bool -> transition -> state -> (state * (exn * string) option) option
   106 (** toplevel state **)
   108 (** toplevel state **)
   107 
   109 
   108 (* datatype state *)
   110 (* datatype state *)
   109 
   111 
   110 datatype node =
   112 datatype node =
   111   Theory of theory * Proof.context option |  (*theory with optional body context*)
   113   Theory of theory * Proof.context option |        (*theory with optional body context*)
   112   Proof of ProofHistory.T |                  (*history of proof states*)
   114   Proof of ProofHistory.T * theory |               (*history of proof states, original theory*)
   113   SkipProof of int History.T * theory;       (*history of proof depths*)
   115   SkipProof of (int History.T * theory) * theory;
       
   116     (*history of proof depths, resulting theory, original theory*)
   114 
   117 
   115 datatype state = State of (node History.T * ((node -> unit) * (node -> unit))) option;
   118 datatype state = State of (node History.T * ((node -> unit) * (node -> unit))) option;
   116 
   119 
   117 val toplevel = State NONE;
   120 val toplevel = State NONE;
   118 
   121 
   121 
   124 
   122 fun level (State NONE) = 0
   125 fun level (State NONE) = 0
   123   | level (State (SOME (node, _))) =
   126   | level (State (SOME (node, _))) =
   124       (case History.current node of
   127       (case History.current node of
   125         Theory _ => 0
   128         Theory _ => 0
   126       | Proof prf => Proof.level (ProofHistory.current prf)
   129       | Proof (prf, _) => Proof.level (ProofHistory.current prf)
   127       | SkipProof (h, _) => History.current h + 1);    (*different notion of proof depth!*)
   130       | SkipProof ((h, _), _) => History.current h + 1);    (*different notion of proof depth!*)
   128 
   131 
   129 fun str_of_state (State NONE) = "at top level"
   132 fun str_of_state (State NONE) = "at top level"
   130   | str_of_state (State (SOME (node, _))) =
   133   | str_of_state (State (SOME (node, _))) =
   131       (case History.current node of
   134       (case History.current node of
   132         Theory _ => "in theory mode"
   135         Theory _ => "in theory mode"
   147 val node_of = History.current o node_history_of;
   150 val node_of = History.current o node_history_of;
   148 
   151 
   149 fun node_case f g state =
   152 fun node_case f g state =
   150   (case node_of state of
   153   (case node_of state of
   151     Theory (thy, _) => f thy
   154     Theory (thy, _) => f thy
   152   | Proof prf => g (ProofHistory.current prf)
   155   | Proof (prf, _) => g (ProofHistory.current prf)
   153   | SkipProof (_, thy) => f thy);
   156   | SkipProof ((_, thy), _) => f thy);
   154 
   157 
   155 val theory_of = node_case I Proof.theory_of;
   158 val theory_of = node_case I Proof.theory_of;
   156 val sign_of = theory_of;
   159 val sign_of = theory_of;
   157 
   160 
   158 fun body_context_of state =
   161 fun body_context_of state =
   187 fun pretty_state_context state =
   190 fun pretty_state_context state =
   188   (case try theory_of state of NONE => []
   191   (case try theory_of state of NONE => []
   189   | SOME thy => pretty_context thy);
   192   | SOME thy => pretty_context thy);
   190 
   193 
   191 fun pretty_node prf_only (Theory (thy, _)) = if prf_only then [] else pretty_context thy
   194 fun pretty_node prf_only (Theory (thy, _)) = if prf_only then [] else pretty_context thy
   192   | pretty_node _ (Proof prf) =
   195   | pretty_node _ (Proof (prf, _)) =
   193       Proof.pretty_state (ProofHistory.position prf) (ProofHistory.current prf)
   196       Proof.pretty_state (ProofHistory.position prf) (ProofHistory.current prf)
   194   | pretty_node _ (SkipProof (h, _)) =
   197   | pretty_node _ (SkipProof ((h, _), _)) =
   195       [Pretty.str ("skipped proof: depth " ^ string_of_int (History.current h))];
   198       [Pretty.str ("skipped proof: depth " ^ string_of_int (History.current h))];
   196 
   199 
   197 fun pretty_state prf_only state =
   200 fun pretty_state prf_only state =
   198   let val ref (begin_state, end_state, _) = Display.current_goals_markers in
   201   let val ref (begin_state, end_state, _) = Display.current_goals_markers in
   199     (case try node_of state of NONE => []
   202     (case try node_of state of NONE => []
   261   in raise_interrupt (fn () => y := f x) (); ! y end;
   264   in raise_interrupt (fn () => y := f x) (); ! y end;
   262 
   265 
   263 in
   266 in
   264 
   267 
   265 fun transaction _ _ _ (State NONE) = raise UNDEF
   268 fun transaction _ _ _ (State NONE) = raise UNDEF
   266   | transaction int hist f (State (SOME (node, term))) =
   269   | transaction save hist f (State (SOME (node, term))) =
   267       let
   270       let
   268         val cont_node = History.map (checkpoint_node int) node;
   271         val cont_node = History.map (checkpoint_node save) node;
   269         val back_node = History.map (copy_node int) cont_node;
   272         val back_node = History.map (copy_node save) cont_node;
   270         fun state nd = State (SOME (nd, term));
   273         fun state nd = State (SOME (nd, term));
   271         fun normal_state nd = (state nd, NONE);
   274         fun normal_state nd = (state nd, NONE);
   272         fun error_state nd exn = (state nd, SOME exn);
   275         fun error_state nd exn = (state nd, SOME exn);
   273 
   276 
   274         val (result, err) =
   277         val (result, err) =
   275           cont_node
   278           cont_node
   276           |> ((fn nd => f int nd)
   279           |> (f
   277               |> (if hist then History.apply_copy (copy_node int) else History.map)
   280               |> (if hist then History.apply_copy (copy_node save) else History.map)
   278               |> debug_trans
   281               |> debug_trans
   279               |> interruptible
   282               |> interruptible
   280               |> transform_error)
   283               |> transform_error)
   281           |> normal_state
   284           |> normal_state
   282           handle exn => error_state cont_node exn;
   285           handle exn => error_state cont_node exn;
   289 end;
   292 end;
   290 
   293 
   291 
   294 
   292 (* primitive transitions *)
   295 (* primitive transitions *)
   293 
   296 
   294 (*NB: recovery from stale theories is provided only for theory-level
   297 (*Note: Recovery from stale theories is provided only for theory-level
   295   operations via MapCurrent and AppCurrent.  Other node or state
   298   operations via Transaction.  Other node or state operations should
   296   operations should not touch theories at all.
   299   not touch theories at all.  Interrupts are enabled only for Keep and
   297 
   300   Transaction.*)
   298   Interrupts are enabled only for Keep, MapCurrent, and AppCurrent.*)
       
   299 
   301 
   300 datatype trans =
   302 datatype trans =
   301   Reset |                                       (*empty toplevel*)
   303   Reset |                                               (*empty toplevel*)
   302   Init of (bool -> node) * ((node -> unit) * (node -> unit)) |
   304   Init of (bool -> node) * ((node -> unit) * (node -> unit)) |
   303     (*init node; provide exit/kill operation*)
   305                                                         (*init node; with exit/kill operation*)
   304   Exit |                                        (*conclude node*)
   306   Exit |                                                (*conclude node*)
   305   Kill |                                        (*abort node*)
   307   Kill |                                                (*abort node*)
   306   Keep of bool -> state -> unit |               (*peek at state*)
   308   Keep of bool -> state -> unit |                       (*peek at state*)
   307   History of node History.T -> node History.T | (*history operation (undo etc.)*)
   309   History of node History.T -> node History.T |         (*history operation (undo etc.)*)
   308   MapCurrent of bool -> node -> node |          (*change node, bypassing history*)
   310   Transaction of bool * bool * (bool -> node -> node);  (*node transaction*)
   309   AppCurrent of bool -> node -> node;           (*change node, recording history*)
       
   310 
   311 
   311 fun undo_limit int = if int then NONE else SOME 0;
   312 fun undo_limit int = if int then NONE else SOME 0;
   312 
   313 
   313 local
   314 local
   314 
   315 
   323   | apply_tr _ Kill (State (SOME (node, (_, kill)))) =
   324   | apply_tr _ Kill (State (SOME (node, (_, kill)))) =
   324       (kill (History.current node); State NONE)
   325       (kill (History.current node); State NONE)
   325   | apply_tr int (Keep f) state = (raise_interrupt (f int) state; state)
   326   | apply_tr int (Keep f) state = (raise_interrupt (f int) state; state)
   326   | apply_tr _ (History _) (State NONE) = raise UNDEF
   327   | apply_tr _ (History _) (State NONE) = raise UNDEF
   327   | apply_tr _ (History f) (State (SOME (node, term))) = State (SOME (f node, term))
   328   | apply_tr _ (History f) (State (SOME (node, term))) = State (SOME (f node, term))
   328   | apply_tr int (MapCurrent f) state = transaction int false f state
   329   | apply_tr int (Transaction (save, hist, f)) state =
   329   | apply_tr int (AppCurrent f) state = transaction int true f state;
   330       transaction (int orelse save) hist (fn x => f int x) state;
   330 
   331 
   331 fun apply_union _ [] state = raise FAILURE (state, UNDEF)
   332 fun apply_union _ [] state = raise FAILURE (state, UNDEF)
   332   | apply_union int (tr :: trs) state =
   333   | apply_union int (tr :: trs) state =
   333       transform_error (apply_tr int tr) state
   334       transform_error (apply_tr int tr) state
   334         handle UNDEF => apply_union int trs state
   335         handle UNDEF => apply_union int trs state
   414 fun init f exit kill = add_trans (Init (f, (exit, kill)));
   415 fun init f exit kill = add_trans (Init (f, (exit, kill)));
   415 val exit = add_trans Exit;
   416 val exit = add_trans Exit;
   416 val kill = add_trans Kill;
   417 val kill = add_trans Kill;
   417 val keep' = add_trans o Keep;
   418 val keep' = add_trans o Keep;
   418 val history = add_trans o History;
   419 val history = add_trans o History;
   419 val map_current = add_trans o MapCurrent;
   420 fun map_current f = add_trans (Transaction (false, false, f));
   420 val app_current = add_trans o AppCurrent;
   421 fun app_current save f = add_trans (Transaction (save, true, f));
   421 
   422 
   422 fun keep f = add_trans (Keep (fn _ => f));
   423 fun keep f = add_trans (Keep (fn _ => f));
   423 fun imperative f = keep (fn _ => f ());
   424 fun imperative f = keep (fn _ => f ());
   424 
   425 
   425 fun init_theory f exit kill =
   426 fun init_theory f exit kill =
   428     (fn Theory (thy, _) => kill thy | _ => raise UNDEF);
   429     (fn Theory (thy, _) => kill thy | _ => raise UNDEF);
   429 
   430 
   430 
   431 
   431 (* typed transitions *)
   432 (* typed transitions *)
   432 
   433 
   433 fun theory f = app_current (K (fn Theory (thy, _) => Theory (f thy, NONE) | _ => raise UNDEF));
   434 fun theory f = app_current false
   434 fun theory' f = app_current (fn int =>
   435   (K (fn Theory (thy, _) => Theory (f thy, NONE) | _ => raise UNDEF));
       
   436 
       
   437 fun theory' f = app_current false (fn int =>
   435   (fn Theory (thy, _) => Theory (f int thy, NONE) | _ => raise UNDEF));
   438   (fn Theory (thy, _) => Theory (f int thy, NONE) | _ => raise UNDEF));
   436 
   439 
   437 fun theory_context f =
   440 fun theory_context f = app_current false
   438   app_current (K (fn Theory (thy, _) => Theory (apsnd SOME (f thy)) | _ => raise UNDEF));
   441   (K (fn Theory (thy, _) => Theory (apsnd SOME (f thy)) | _ => raise UNDEF));
   439 
   442 
   440 fun theory_to_proof f = app_current (fn int =>
   443 fun to_proof save f = app_current save (fn int =>
   441   (fn Theory (thy, _) =>
   444   (fn Theory (thy, _) =>
   442       let val st = f thy in
   445       let val st = f thy in
   443         if Theory.eq_thy (thy, Proof.theory_of st) then ()
   446         if save orelse Theory.eq_thy (thy, Proof.theory_of st) then ()
   444         else error "Theory changed at start of proof";
   447         else error "Theory changed at start of proof";
   445         if ! skip_proofs then
   448         if ! skip_proofs then
   446           SkipProof (History.init (undo_limit int) 0, #1 (Proof.global_skip_proof int st))
   449           SkipProof ((History.init (undo_limit int) 0, #1 (Proof.global_skip_proof int st)), thy)
   447         else Proof (ProofHistory.init (undo_limit int) st)
   450         else Proof (ProofHistory.init (undo_limit int) st, thy)
   448       end
   451       end
   449     | _ => raise UNDEF));
   452     | _ => raise UNDEF));
   450 
   453 
       
   454 val theory_to_proof = to_proof false;
       
   455 val theory_theory_to_proof = to_proof true;
       
   456 
   451 fun proofs' f = map_current (fn int =>
   457 fun proofs' f = map_current (fn int =>
   452   (fn Proof prf => Proof (ProofHistory.applys (f int) prf)
   458   (fn Proof (prf, orig_thy) => Proof (ProofHistory.applys (f int) prf, orig_thy)
   453     | SkipProof (h, thy) => SkipProof (History.apply I h, thy)
   459     | SkipProof ((h, thy), orig_thy) => SkipProof ((History.apply I h, thy), orig_thy)
   454     | _ => raise UNDEF));
   460     | _ => raise UNDEF));
   455 
   461 
   456 fun proof' f = proofs' (Seq.single oo f);
   462 fun proof' f = proofs' (Seq.single oo f);
   457 val proofs = proofs' o K;
   463 val proofs = proofs' o K;
   458 val proof = proof' o K;
   464 val proof = proof' o K;
   459 
   465 
   460 fun actual_proof f = map_current (fn _ =>
   466 fun actual_proof f = map_current (fn _ =>
   461   (fn Proof prf => Proof (f prf) | _ => raise UNDEF));
   467   (fn Proof (prf, orig_thy) => Proof (f prf, orig_thy) | _ => raise UNDEF));
   462 
   468 
   463 fun skip_proof f = map_current (fn _ =>
   469 fun skip_proof f = map_current (fn _ =>
   464   (fn SkipProof (h, thy) => SkipProof (f h, thy) | _ => raise UNDEF));
   470   (fn SkipProof ((h, thy), orig_thy) => SkipProof ((f h, thy), orig_thy) | _ => raise UNDEF));
   465 
   471 
   466 fun end_proof f = map_current (fn int =>
   472 fun end_proof f = map_current (fn int =>
   467   (fn Proof prf => Theory (f int (ProofHistory.current prf))
   473   (fn Proof (prf, _) => Theory (f int (ProofHistory.current prf))
   468     | SkipProof (h, thy) => if History.current h = 0 then Theory (thy, NONE) else raise UNDEF
   474     | SkipProof ((h, thy), _) => if History.current h = 0 then Theory (thy, NONE) else raise UNDEF
       
   475     | _ => raise UNDEF));
       
   476 
       
   477 val forget_proof = map_current (fn _ =>
       
   478   (fn Proof (_, orig_thy) => Theory (orig_thy, NONE)
       
   479     | SkipProof (_, orig_thy) => Theory (orig_thy, NONE)
   469     | _ => raise UNDEF));
   480     | _ => raise UNDEF));
   470 
   481 
   471 fun proof_to_theory' f = end_proof (rpair NONE oo f);
   482 fun proof_to_theory' f = end_proof (rpair NONE oo f);
   472 fun proof_to_theory f = proof_to_theory' (K f);
   483 fun proof_to_theory f = proof_to_theory' (K f);
   473 fun proof_to_theory_context f = end_proof (apsnd SOME oo f);
   484 fun proof_to_theory_context f = end_proof (apsnd SOME oo f);
   474 
   485 
   475 fun skip_proof_to_theory p = map_current (fn _ =>
   486 fun skip_proof_to_theory p = map_current (fn _ =>
   476   (fn SkipProof (h, thy) =>
   487   (fn SkipProof ((h, thy), _) =>
   477     if p (History.current h) then Theory (thy, NONE)
   488     if p (History.current h) then Theory (thy, NONE)
   478     else raise UNDEF
   489     else raise UNDEF
   479   | _ => raise UNDEF));
   490   | _ => raise UNDEF));
   480 
   491 
   481 val unknown_theory = imperative (fn () => warning "Unknown theory context");
   492 val unknown_theory = imperative (fn () => warning "Unknown theory context");