src/Pure/Isar/toplevel.ML
changeset 18592 451d622bb4a9
parent 18589 c27c9fa9e83d
child 18641 688056d430b0
equal deleted inserted replaced
18591:04b9f2bf5a48 18592:451d622bb4a9
    72     -> transition -> transition
    72     -> transition -> transition
    73   val theory: (theory -> theory) -> transition -> transition
    73   val theory: (theory -> theory) -> transition -> transition
    74   val theory': (bool -> theory -> theory) -> transition -> transition
    74   val theory': (bool -> theory -> theory) -> transition -> transition
    75   val theory_context: (theory -> theory * Proof.context) -> transition -> transition
    75   val theory_context: (theory -> theory * Proof.context) -> transition -> transition
    76   val theory_to_proof: (theory -> Proof.state) -> transition -> transition
    76   val theory_to_proof: (theory -> Proof.state) -> transition -> transition
    77   val theory_theory_to_proof: (theory -> Proof.state) -> transition -> transition
       
    78   val proof: (Proof.state -> Proof.state) -> transition -> transition
    77   val proof: (Proof.state -> Proof.state) -> transition -> transition
    79   val proofs: (Proof.state -> Proof.state Seq.seq) -> transition -> transition
    78   val proofs: (Proof.state -> Proof.state Seq.seq) -> transition -> transition
    80   val proof': (bool -> Proof.state -> Proof.state) -> transition -> transition
    79   val proof': (bool -> Proof.state -> Proof.state) -> transition -> transition
    81   val proofs': (bool -> Proof.state -> Proof.state Seq.seq) -> transition -> transition
    80   val proofs': (bool -> Proof.state -> Proof.state Seq.seq) -> transition -> transition
    82   val actual_proof: (ProofHistory.T -> ProofHistory.T) -> transition -> transition
    81   val actual_proof: (ProofHistory.T -> ProofHistory.T) -> transition -> transition
   251 
   250 
   252 fun is_stale state = Context.is_stale (theory_of state) handle UNDEF => false;
   251 fun is_stale state = Context.is_stale (theory_of state) handle UNDEF => false;
   253 
   252 
   254 val stale_theory = ERROR_MESSAGE "Stale theory encountered after succesful execution!";
   253 val stale_theory = ERROR_MESSAGE "Stale theory encountered after succesful execution!";
   255 
   254 
   256 fun checkpoint_node true (Theory (thy, ctxt)) = Theory (Theory.checkpoint thy, ctxt)
   255 fun checkpoint_node (Theory (thy, ctxt)) = Theory (Theory.checkpoint thy, ctxt)
   257   | checkpoint_node _ node = node;
   256   | checkpoint_node node = node;
   258 
   257 
   259 fun copy_node true (Theory (thy, ctxt)) = Theory (Theory.copy thy, ctxt)
   258 fun copy_node (Theory (thy, ctxt)) = Theory (Theory.copy thy, ctxt)
   260   | copy_node _ node = node;
   259   | copy_node node = node;
   261 
   260 
   262 fun return (result, NONE) = result
   261 fun return (result, NONE) = result
   263   | return (result, SOME exn) = raise FAILURE (result, exn);
   262   | return (result, SOME exn) = raise FAILURE (result, exn);
   264 
   263 
   265 fun debug_trans f x =
   264 fun debug_trans f x =
   272   let val y = ref x
   271   let val y = ref x
   273   in raise_interrupt (fn () => y := f x) (); ! y end;
   272   in raise_interrupt (fn () => y := f x) (); ! y end;
   274 
   273 
   275 in
   274 in
   276 
   275 
   277 fun transaction _ _ _ (State NONE) = raise UNDEF
   276 fun transaction _ _ (State NONE) = raise UNDEF
   278   | transaction save hist f (State (SOME (node, term))) =
   277   | transaction hist f (State (SOME (node, term))) =
   279       let
   278       let
   280         val save = true;  (* FIXME *)
   279         val cont_node = History.map checkpoint_node node;
   281         val cont_node = History.map (checkpoint_node save) node;
   280         val back_node = History.map copy_node cont_node;
   282         val back_node = History.map (copy_node save) cont_node;
       
   283         fun state nd = State (SOME (nd, term));
   281         fun state nd = State (SOME (nd, term));
   284         fun normal_state nd = (state nd, NONE);
   282         fun normal_state nd = (state nd, NONE);
   285         fun error_state nd exn = (state nd, SOME exn);
   283         fun error_state nd exn = (state nd, SOME exn);
   286 
   284 
   287         val (result, err) =
   285         val (result, err) =
   288           cont_node
   286           cont_node
   289           |> (f
   287           |> (f
   290               |> (if hist then History.apply_copy (copy_node save) else History.map)
   288               |> (if hist then History.apply_copy copy_node else History.map)
   291               |> debug_trans
   289               |> debug_trans
   292               |> interruptible
   290               |> interruptible
   293               |> transform_error)
   291               |> transform_error)
   294           |> normal_state
   292           |> normal_state
   295           handle exn => error_state cont_node exn;
   293           handle exn => error_state cont_node exn;
   301 
   299 
   302 fun mapping f (State (SOME (node, term))) = State (SOME (History.map f node, term))
   300 fun mapping f (State (SOME (node, term))) = State (SOME (History.map f node, term))
   303   | mapping _ state = state;
   301   | mapping _ state = state;
   304 
   302 
   305 val no_body_context = mapping (fn Theory (thy, _) => Theory (thy, NONE) | node => node);
   303 val no_body_context = mapping (fn Theory (thy, _) => Theory (thy, NONE) | node => node);
   306 val checkpoint = mapping (checkpoint_node true);
   304 val checkpoint = mapping checkpoint_node;
   307 val copy = mapping (copy_node true);
   305 val copy = mapping copy_node;
   308 
   306 
   309 end;
   307 end;
   310 
   308 
   311 
   309 
   312 (* primitive transitions *)
   310 (* primitive transitions *)
   322                                                         (*init node; with exit/kill operation*)
   320                                                         (*init node; with exit/kill operation*)
   323   Exit |                                                (*conclude node*)
   321   Exit |                                                (*conclude node*)
   324   Kill |                                                (*abort node*)
   322   Kill |                                                (*abort node*)
   325   Keep of bool -> state -> unit |                       (*peek at state*)
   323   Keep of bool -> state -> unit |                       (*peek at state*)
   326   History of node History.T -> node History.T |         (*history operation (undo etc.)*)
   324   History of node History.T -> node History.T |         (*history operation (undo etc.)*)
   327   Transaction of bool * bool * (bool -> node -> node);  (*node transaction*)
   325   Transaction of bool * (bool -> node -> node);         (*node transaction*)
   328 
   326 
   329 fun undo_limit int = if int then NONE else SOME 0;
   327 fun undo_limit int = if int then NONE else SOME 0;
   330 
   328 
   331 local
   329 local
   332 
   330 
   341   | apply_tr _ Kill (State (SOME (node, (_, kill)))) =
   339   | apply_tr _ Kill (State (SOME (node, (_, kill)))) =
   342       (kill (History.current node); State NONE)
   340       (kill (History.current node); State NONE)
   343   | apply_tr int (Keep f) state = (raise_interrupt (f int) state; state)
   341   | apply_tr int (Keep f) state = (raise_interrupt (f int) state; state)
   344   | apply_tr _ (History _) (State NONE) = raise UNDEF
   342   | apply_tr _ (History _) (State NONE) = raise UNDEF
   345   | apply_tr _ (History f) (State (SOME (node, term))) = State (SOME (f node, term))
   343   | apply_tr _ (History f) (State (SOME (node, term))) = State (SOME (f node, term))
   346   | apply_tr int (Transaction (save, hist, f)) state =
   344   | apply_tr int (Transaction (hist, f)) state = transaction hist (fn x => f int x) state;
   347       transaction (int orelse save) hist (fn x => f int x) state;
       
   348 
   345 
   349 fun apply_union _ [] state = raise FAILURE (state, UNDEF)
   346 fun apply_union _ [] state = raise FAILURE (state, UNDEF)
   350   | apply_union int (tr :: trs) state =
   347   | apply_union int (tr :: trs) state =
   351       transform_error (apply_tr int tr) state
   348       transform_error (apply_tr int tr) state
   352         handle UNDEF => apply_union int trs state
   349         handle UNDEF => apply_union int trs state
   432 fun init f exit kill = add_trans (Init (f, (exit, kill)));
   429 fun init f exit kill = add_trans (Init (f, (exit, kill)));
   433 val exit = add_trans Exit;
   430 val exit = add_trans Exit;
   434 val kill = add_trans Kill;
   431 val kill = add_trans Kill;
   435 val keep' = add_trans o Keep;
   432 val keep' = add_trans o Keep;
   436 val history = add_trans o History;
   433 val history = add_trans o History;
   437 fun map_current f = add_trans (Transaction (false, false, f));
   434 fun map_current f = add_trans (Transaction (false, f));
   438 fun app_current save f = add_trans (Transaction (save, true, f));
   435 fun app_current f = add_trans (Transaction (true, f));
   439 
   436 
   440 fun keep f = add_trans (Keep (fn _ => f));
   437 fun keep f = add_trans (Keep (fn _ => f));
   441 fun imperative f = keep (fn _ => f ());
   438 fun imperative f = keep (fn _ => f ());
   442 
   439 
   443 fun init_theory f exit kill =
   440 fun init_theory f exit kill =
   446     (fn Theory (thy, _) => kill thy | _ => raise UNDEF);
   443     (fn Theory (thy, _) => kill thy | _ => raise UNDEF);
   447 
   444 
   448 
   445 
   449 (* typed transitions *)
   446 (* typed transitions *)
   450 
   447 
   451 fun theory f = app_current false
   448 fun theory f = app_current
   452   (K (fn Theory (thy, _) => Theory (f thy, NONE) | _ => raise UNDEF));
   449   (K (fn Theory (thy, _) => Theory (f thy, NONE) | _ => raise UNDEF));
   453 
   450 
   454 fun theory' f = app_current false (fn int =>
   451 fun theory' f = app_current (fn int =>
   455   (fn Theory (thy, _) => Theory (f int thy, NONE) | _ => raise UNDEF));
   452   (fn Theory (thy, _) => Theory (f int thy, NONE) | _ => raise UNDEF));
   456 
   453 
   457 fun theory_context f = app_current false
   454 fun theory_context f = app_current
   458   (K (fn Theory (thy, _) => Theory (apsnd SOME (f thy)) | _ => raise UNDEF));
   455   (K (fn Theory (thy, _) => Theory (apsnd SOME (f thy)) | _ => raise UNDEF));
   459 
   456 
   460 fun to_proof save f = app_current save (fn int =>
   457 fun theory_to_proof f = app_current (fn int =>
   461   (fn Theory (thy, _) =>
   458   (fn Theory (thy, _) =>
   462       let val st = f thy in
   459     if ! skip_proofs then
   463         if save orelse Theory.eq_thy (thy, Proof.theory_of st) then ()
   460       SkipProof ((History.init (undo_limit int) 0, #1 (Proof.global_skip_proof int (f thy))), thy)
   464         else error "Theory changed at start of proof";
   461     else Proof (ProofHistory.init (undo_limit int) (f thy), thy)
   465         if ! skip_proofs then
   462   | _ => raise UNDEF));
   466           SkipProof ((History.init (undo_limit int) 0, #1 (Proof.global_skip_proof int st)), thy)
       
   467         else Proof (ProofHistory.init (undo_limit int) st, thy)
       
   468       end
       
   469     | _ => raise UNDEF));
       
   470 
       
   471 val theory_to_proof = to_proof false;
       
   472 val theory_theory_to_proof = to_proof true;
       
   473 
   463 
   474 fun proofs' f = map_current (fn int =>
   464 fun proofs' f = map_current (fn int =>
   475   (fn Proof (prf, orig_thy) => Proof (ProofHistory.applys (f int) prf, orig_thy)
   465   (fn Proof (prf, orig_thy) => Proof (ProofHistory.applys (f int) prf, orig_thy)
   476     | SkipProof ((h, thy), orig_thy) => SkipProof ((History.apply I h, thy), orig_thy)
   466     | SkipProof ((h, thy), orig_thy) => SkipProof ((History.apply I h, thy), orig_thy)
   477     | _ => raise UNDEF));
   467     | _ => raise UNDEF));