src/Pure/Isar/proof.ML
changeset 6404 2daaf2943c79
parent 6262 0ebfcf181d84
child 6529 0f4c2ebc5018
equal deleted inserted replaced
6403:86e9d24f4238 6404:2daaf2943c79
    53   val show_i: string -> context attribute list -> term * term list -> state -> state
    53   val show_i: string -> context attribute list -> term * term list -> state -> state
    54   val have: string -> context attribute list -> string * string list -> state -> state
    54   val have: string -> context attribute list -> string * string list -> state -> state
    55   val have_i: string -> context attribute list -> term * term list -> state -> state
    55   val have_i: string -> context attribute list -> term * term list -> state -> state
    56   val begin_block: state -> state
    56   val begin_block: state -> state
    57   val next_block: state -> state
    57   val next_block: state -> state
    58   val end_block: (context -> method) -> state -> state Seq.seq
    58   val end_block: state -> state
    59   val qed: (context -> method) -> bstring option -> theory attribute list option -> state
    59   val at_bottom: state -> bool
    60     -> theory * (string * string * thm)
    60   val local_qed: (state -> state Seq.seq) -> state -> state Seq.seq
       
    61   val global_qed: (state -> state Seq.seq) -> bstring option
       
    62     -> theory attribute list option -> state -> theory * (string * string * thm)
    61 end;
    63 end;
    62 
    64 
    63 signature PROOF_PRIVATE =
    65 signature PROOF_PRIVATE =
    64 sig
    66 sig
    65   include PROOF
    67   include PROOF
   553 (* current goal *)
   555 (* current goal *)
   554 
   556 
   555 fun current_goal (State ({goal = Some goal, ...}, _)) = goal
   557 fun current_goal (State ({goal = Some goal, ...}, _)) = goal
   556   | current_goal state = raise STATE ("No current goal!", state);
   558   | current_goal state = raise STATE ("No current goal!", state);
   557 
   559 
   558 fun assert_current_goal state = (current_goal state; state);
   560 fun assert_current_goal true (state as State ({goal = None, ...}, _)) =
       
   561       raise STATE ("No goal in this block!", state)
       
   562   | assert_current_goal false (state as State ({goal = Some _, ...}, _)) =
       
   563       raise STATE ("Goal present in this block!", state)
       
   564   | assert_current_goal _ state = state;
   559 
   565 
   560 fun assert_bottom true (state as State (_, _ :: _)) =
   566 fun assert_bottom true (state as State (_, _ :: _)) =
   561       raise STATE ("Not at bottom of proof!", state)
   567       raise STATE ("Not at bottom of proof!", state)
   562   | assert_bottom false (state as State (_, [])) =
   568   | assert_bottom false (state as State (_, [])) =
   563       raise STATE ("Already at bottom of proof!", state)
   569       raise STATE ("Already at bottom of proof!", state)
   564   | assert_bottom _ state = state;
   570   | assert_bottom _ state = state;
   565 
   571 
       
   572 val at_bottom = can (assert_bottom true o close_block);
       
   573 
   566 
   574 
   567 (* finish proof *)
   575 (* finish proof *)
   568 
   576 
   569 fun check_finished state states =
   577 fun check_finished state states =
   570   (case Seq.pull states of
   578   (case Seq.pull states of
   571     None => raise STATE ("Failed to finish proof", state)
   579     None => raise STATE ("Failed to finish proof", state)
   572   | Some s_sq => Seq.cons s_sq);
   580   | Some s_sq => Seq.cons s_sq);
   573 
   581 
   574 fun finish_proof bot meth_fun state =
   582 fun finish_proof bot finalize state =
   575   state
   583   state
   576   |> assert_forward
   584   |> assert_forward
   577   |> close_block
   585   |> close_block
   578   |> assert_bottom bot
   586   |> assert_bottom bot
   579   |> assert_current_goal
   587   |> assert_current_goal true
   580   |> refine meth_fun
   588   |> finalize
   581   |> check_finished state;
   589   |> check_finished state;
   582 
   590 
   583 
   591 
   584 (* conclude local proof *)
   592 (* end_block *)
       
   593 
       
   594 fun end_block state =
       
   595   state
       
   596   |> assert_forward
       
   597   |> close_block
       
   598   |> assert_current_goal false
       
   599   |> close_block
       
   600   |> fetch_facts state;
       
   601 
       
   602 
       
   603 (* local_qed *)
   585 
   604 
   586 fun finish_local state =
   605 fun finish_local state =
   587   let
   606   let
   588     val ((kind, name, asms, t), (_, raw_thm)) = current_goal state;
   607     val ((kind, name, asms, t), (_, raw_thm)) = current_goal state;
   589     val result = prep_result state asms t raw_thm;
   608     val result = prep_result state asms t raw_thm;
   597     |> close_block
   616     |> close_block
   598     |> have_thmss name atts [Thm.no_attributes [result]]
   617     |> have_thmss name atts [Thm.no_attributes [result]]
   599     |> opt_solve
   618     |> opt_solve
   600   end;
   619   end;
   601 
   620 
   602 fun end_block meth_fun state =
   621 fun local_qed finalize state =
   603   if can assert_current_goal (close_block state) then
   622   state
   604     state
   623   |> finish_proof false finalize
   605     |> finish_proof false meth_fun
   624   |> (Seq.flat o Seq.map finish_local);
   606     |> (Seq.flat o Seq.map finish_local)
   625 
   607   else
   626 
   608     state
   627 (* global_qed *)
   609     |> assert_forward
       
   610     |> close_block
       
   611     |> close_block
       
   612     |> fetch_facts state
       
   613     |> Seq.single;
       
   614 
       
   615 
       
   616 (* conclude global proof *)
       
   617 
   628 
   618 fun finish_global alt_name alt_atts state =
   629 fun finish_global alt_name alt_atts state =
   619   let
   630   let
   620     val ((kind, def_name, asms, t), (_, raw_thm)) = current_goal state;
   631     val ((kind, def_name, asms, t), (_, raw_thm)) = current_goal state;
   621     val result = final_result state (prep_result state asms t raw_thm);
   632     val result = final_result state (prep_result state asms t raw_thm);
   628       | _ => raise STATE ("No global goal!", state));
   639       | _ => raise STATE ("No global goal!", state));
   629 
   640 
   630     val (thy', result') = PureThy.store_thm ((name, result), atts) (theory_of state);
   641     val (thy', result') = PureThy.store_thm ((name, result), atts) (theory_of state);
   631   in (thy', (kind_name kind, name, result')) end;
   642   in (thy', (kind_name kind, name, result')) end;
   632 
   643 
   633 fun qed meth_fun alt_name alt_atts state =
   644 fun global_qed finalize alt_name alt_atts state =
   634   state
   645   state
   635   |> finish_proof true meth_fun
   646   |> finish_proof true finalize
   636   |> Seq.hd
   647   |> Seq.hd
   637   |> finish_global alt_name alt_atts;
   648   |> finish_global alt_name alt_atts;
   638 
   649 
   639 
   650 
   640 end;
   651 end;