src/Pure/Isar/proof.ML
changeset 61841 4d3527b94f2a
parent 61659 ffee6aea0ff2
child 62663 bea354f6ff21
equal deleted inserted replaced
61840:a3793600cb93 61841:4d3527b94f2a
    33   val enter_chain: state -> state
    33   val enter_chain: state -> state
    34   val enter_backward: state -> state
    34   val enter_backward: state -> state
    35   val has_bottom_goal: state -> bool
    35   val has_bottom_goal: state -> bool
    36   val using_facts: thm list -> state -> state
    36   val using_facts: thm list -> state -> state
    37   val pretty_state: state -> Pretty.T list
    37   val pretty_state: state -> Pretty.T list
    38   val refine: Method.text -> state -> state Seq.seq
    38   val refine: Method.text -> state -> state Seq.result Seq.seq
    39   val refine_end: Method.text -> state -> state Seq.seq
    39   val refine_end: Method.text -> state -> state Seq.result Seq.seq
       
    40   val refine_singleton: Method.text -> state -> state
    40   val refine_insert: thm list -> state -> state
    41   val refine_insert: thm list -> state -> state
    41   val refine_primitive: (Proof.context -> thm -> thm) -> state -> state
    42   val refine_primitive: (Proof.context -> thm -> thm) -> state -> state
    42   val raw_goal: state -> {context: context, facts: thm list, goal: thm}
    43   val raw_goal: state -> {context: context, facts: thm list, goal: thm}
    43   val goal: state -> {context: context, facts: thm list, goal: thm}
    44   val goal: state -> {context: context, facts: thm list, goal: thm}
    44   val simple_goal: state -> {context: context, goal: thm}
    45   val simple_goal: state -> {context: context, goal: thm}
    88   val begin_block: state -> state
    89   val begin_block: state -> state
    89   val next_block: state -> state
    90   val next_block: state -> state
    90   val end_block: state -> state
    91   val end_block: state -> state
    91   val begin_notepad: context -> state
    92   val begin_notepad: context -> state
    92   val end_notepad: state -> context
    93   val end_notepad: state -> context
    93   val proof: Method.text option -> state -> state Seq.seq
    94   val proof: Method.text_range option -> state -> state Seq.result Seq.seq
    94   val proof_results: Method.text_range option -> state -> state Seq.result Seq.seq
       
    95   val defer: int -> state -> state
    95   val defer: int -> state -> state
    96   val prefer: int -> state -> state
    96   val prefer: int -> state -> state
    97   val apply: Method.text -> state -> state Seq.seq
    97   val apply: Method.text_range -> state -> state Seq.result Seq.seq
    98   val apply_end: Method.text -> state -> state Seq.seq
    98   val apply_end: Method.text_range -> state -> state Seq.result Seq.seq
    99   val apply_results: Method.text_range -> state -> state Seq.result Seq.seq
       
   100   val apply_end_results: Method.text_range -> state -> state Seq.result Seq.seq
       
   101   val local_qed: Method.text_range option * bool -> state -> state
    99   val local_qed: Method.text_range option * bool -> state -> state
   102   val theorem: Method.text option -> (thm list list -> context -> context) ->
   100   val theorem: Method.text option -> (thm list list -> context -> context) ->
   103     (term * term list) list list -> context -> state
   101     (term * term list) list list -> context -> state
   104   val theorem_cmd: Method.text option -> (thm list list -> context -> context) ->
   102   val theorem_cmd: Method.text option -> (thm list list -> context -> context) ->
   105     (string * string list) list list -> context -> state
   103     (string * string list) list list -> context -> state
   422 fun goal_cases ctxt st =
   420 fun goal_cases ctxt st =
   423   Rule_Cases.make_common ctxt (Thm.prop_of st) (map (rpair [] o rpair []) (goals st));
   421   Rule_Cases.make_common ctxt (Thm.prop_of st) (map (rpair [] o rpair []) (goals st));
   424 
   422 
   425 fun apply_method text ctxt state =
   423 fun apply_method text ctxt state =
   426   find_goal state |> (fn (goal_ctxt, {statement, using, goal, before_qed, after_qed}) =>
   424   find_goal state |> (fn (goal_ctxt, {statement, using, goal, before_qed, after_qed}) =>
   427     Method.evaluate text ctxt using goal
   425     Method.evaluate text ctxt using (goal_ctxt, goal)
   428     |> Seq.map (fn (meth_cases, goal') =>
   426     |> Seq.map_result (fn (goal_ctxt', goal') =>
   429       let
   427       let
   430         val goal_ctxt' = goal_ctxt
   428         val goal_ctxt'' = goal_ctxt'
   431           |> Proof_Context.update_cases_legacy (no_goal_cases goal @ goal_cases ctxt goal')
   429           |> Proof_Context.update_cases_legacy (no_goal_cases goal @ goal_cases ctxt goal');
   432           |> Proof_Context.update_cases meth_cases;
       
   433         val state' = state
   430         val state' = state
   434           |> map_goal (K (goal_ctxt', statement, using, goal', before_qed, after_qed));
   431           |> map_goal (K (goal_ctxt'', statement, using, goal', before_qed, after_qed));
   435       in state' end));
   432       in state' end));
   436 
   433 
   437 in
   434 in
   438 
   435 
   439 fun refine text state = apply_method text (context_of state) state;
   436 fun refine text state = apply_method text (context_of state) state;
   440 fun refine_end text state = apply_method text (#1 (find_goal state)) state;
   437 fun refine_end text state = apply_method text (#1 (find_goal state)) state;
   441 
   438 
       
   439 fun refine_singleton text = refine text #> Seq.the_result "";
       
   440 
   442 fun refine_insert ths =
   441 fun refine_insert ths =
   443   Seq.hd o refine (Method.Basic (K (Method.insert ths)));
   442   refine_singleton (Method.Basic (K (Method.insert ths)));
   444 
   443 
   445 fun refine_primitive r =  (* FIXME Seq.Error!? *)
   444 fun refine_primitive r =
   446   Seq.hd o refine (Method.Basic (fn ctxt => fn _ => EMPTY_CASES (PRIMITIVE (r ctxt))));
   445   refine_singleton (Method.Basic (fn ctxt => fn _ => Method.CONTEXT_TACTIC (PRIMITIVE (r ctxt))));
   447 
   446 
   448 end;
   447 end;
   449 
   448 
   450 
   449 
   451 (* refine via sub-proof *)
   450 (* refine via sub-proof *)
   922 
   921 
   923 
   922 
   924 (* sub-proofs *)
   923 (* sub-proofs *)
   925 
   924 
   926 fun proof opt_text =
   925 fun proof opt_text =
   927   assert_backward
   926   Seq.APPEND
   928   #> refine (the_default Method.standard_text opt_text)
   927     (assert_backward
   929   #> Seq.map
   928       #> refine (the_default Method.standard_text (Method.text opt_text))
   930     (using_facts []
   929       #> Seq.map_result
   931       #> enter_forward
   930         (using_facts []
   932       #> open_block
   931           #> enter_forward
   933       #> reset_goal);
   932           #> open_block
   934 
   933           #> reset_goal),
   935 fun proof_results arg =
   934      method_error "initial" (Method.position opt_text));
   936   Seq.APPEND (proof (Method.text arg) #> Seq.make_results,
       
   937     method_error "initial" (Method.position arg));
       
   938 
   935 
   939 fun end_proof bot (prev_pos, (opt_text, immed)) =
   936 fun end_proof bot (prev_pos, (opt_text, immed)) =
   940   let
   937   let
   941     val (finish_text, terminal_pos, finished_pos) =
   938     val (finish_text, terminal_pos, finished_pos) =
   942       (case opt_text of
   939       (case opt_text of
   949       |> assert_bottom bot
   946       |> assert_bottom bot
   950       |> close_block
   947       |> close_block
   951       |> assert_current_goal true
   948       |> assert_current_goal true
   952       |> using_facts []
   949       |> using_facts []
   953       |> `before_qed |-> (refine o the_default Method.succeed_text)
   950       |> `before_qed |-> (refine o the_default Method.succeed_text)
   954       |> Seq.maps (refine finish_text)
   951       |> Seq.maps_results (refine finish_text),
   955       |> Seq.make_results, method_error "terminal" terminal_pos)
   952       method_error "terminal" terminal_pos)
   956     #> Seq.maps_results (Seq.single o finished_goal finished_pos)
   953     #> Seq.maps_results (Seq.single o finished_goal finished_pos)
   957   end;
   954   end;
   958 
   955 
   959 fun check_result msg sq =
   956 fun check_result msg sq =
   960   (case Seq.pull sq of
   957   (case Seq.pull sq of
   964 
   961 
   965 (* unstructured refinement *)
   962 (* unstructured refinement *)
   966 
   963 
   967 fun defer i =
   964 fun defer i =
   968   assert_no_chain #>
   965   assert_no_chain #>
   969   refine (Method.Basic (fn _ => METHOD (fn _ => ASSERT_SUBGOAL defer_tac i))) #> Seq.hd;
   966   refine_singleton (Method.Basic (fn _ => METHOD (fn _ => ASSERT_SUBGOAL defer_tac i)));
   970 
   967 
   971 fun prefer i =
   968 fun prefer i =
   972   assert_no_chain #>
   969   assert_no_chain #>
   973   refine (Method.Basic (fn _ => METHOD (fn _ => ASSERT_SUBGOAL prefer_tac i))) #> Seq.hd;
   970   refine_singleton (Method.Basic (fn _ => METHOD (fn _ => ASSERT_SUBGOAL prefer_tac i)));
   974 
   971 
   975 fun apply text = assert_backward #> refine text #> Seq.map (using_facts []);
   972 fun apply (text, (pos, _)) =
   976 
   973   Seq.APPEND (assert_backward #> refine text #> Seq.map_result (using_facts []),
   977 fun apply_end text = assert_forward #> refine_end text;
   974     method_error "" pos);
   978 
   975 
   979 fun apply_results (text, (pos, _)) =
   976 fun apply_end (text, (pos, _)) =
   980   Seq.APPEND (apply text #> Seq.make_results, method_error "" pos);
   977   Seq.APPEND (assert_forward #> refine_end text, method_error "" pos);
   981 
       
   982 fun apply_end_results (text, (pos, _)) =
       
   983   Seq.APPEND (apply_end text #> Seq.make_results, method_error "" pos);
       
   984 
   978 
   985 
   979 
   986 
   980 
   987 (** goals **)
   981 (** goals **)
   988 
   982 
  1000     val explicit_vars = fold Term.add_vars var_props [];
   994     val explicit_vars = fold Term.add_vars var_props [];
  1001     val vars = filter_out (member (op =) explicit_vars) (fold Term.add_vars props []);
   995     val vars = filter_out (member (op =) explicit_vars) (fold Term.add_vars props []);
  1002   in map (Logic.mk_term o Var) vars end;
   996   in map (Logic.mk_term o Var) vars end;
  1003 
   997 
  1004 fun refine_terms n =
   998 fun refine_terms n =
  1005   refine (Method.Basic (fn ctxt => EMPTY_CASES o
   999   refine_singleton (Method.Basic (fn ctxt => Method.CONTEXT_TACTIC o
  1006     K (HEADGOAL (PRECISE_CONJUNCTS n
  1000     K (HEADGOAL (PRECISE_CONJUNCTS n
  1007       (HEADGOAL (CONJUNCTS (ALLGOALS (resolve_tac ctxt [Drule.termI]))))))))
  1001       (HEADGOAL (CONJUNCTS (ALLGOALS (resolve_tac ctxt [Drule.termI]))))))));
  1008   #> Seq.hd;
       
  1009 
  1002 
  1010 in
  1003 in
  1011 
  1004 
  1012 fun generic_goal kind before_qed after_qed goal_ctxt goal_propss result_binds state =
  1005 fun generic_goal kind before_qed after_qed goal_ctxt goal_propss result_binds state =
  1013   let
  1006   let
  1040         #> Proof_Context.auto_bind_goal goal_props)
  1033         #> Proof_Context.auto_bind_goal goal_props)
  1041     |> set_goal (make_goal (statement, [], Goal.init goal, before_qed, after_qed'))
  1034     |> set_goal (make_goal (statement, [], Goal.init goal, before_qed, after_qed'))
  1042     |> chaining ? (`the_facts #-> using_facts)
  1035     |> chaining ? (`the_facts #-> using_facts)
  1043     |> reset_facts
  1036     |> reset_facts
  1044     |> not (null vars) ? refine_terms (length goal_propss)
  1037     |> not (null vars) ? refine_terms (length goal_propss)
  1045     |> null goal_props ? (refine (Method.Basic Method.assumption) #> Seq.hd)
  1038     |> null goal_props ? refine_singleton (Method.Basic Method.assumption)
  1046   end;
  1039   end;
  1047 
  1040 
  1048 fun generic_qed state =
  1041 fun generic_qed state =
  1049   let
  1042   let
  1050     val (goal_ctxt, {statement = (_, propss, _), goal, after_qed, ...}) =
  1043     val (goal_ctxt, {statement = (_, propss, _), goal, after_qed, ...}) =
  1149 (* terminal proof steps *)
  1142 (* terminal proof steps *)
  1150 
  1143 
  1151 local
  1144 local
  1152 
  1145 
  1153 fun terminal_proof qeds initial terminal =
  1146 fun terminal_proof qeds initial terminal =
  1154   proof_results (SOME initial) #> Seq.maps_results (qeds (#2 (#2 initial), terminal))
  1147   proof (SOME initial) #> Seq.maps_results (qeds (#2 (#2 initial), terminal))
  1155   #> Seq.the_result "";
  1148   #> Seq.the_result "";
  1156 
  1149 
  1157 in
  1150 in
  1158 
  1151 
  1159 fun local_terminal_proof (text, opt_text) = terminal_proof local_qeds text (opt_text, true);
  1152 fun local_terminal_proof (text, opt_text) = terminal_proof local_qeds text (opt_text, true);