src/Pure/Isar/proof.ML
changeset 12085 235576bea937
parent 12065 5f7f44d5e3dd
child 12096 8945a021f375
equal deleted inserted replaced
12084:2f794ad3c015 12085:235576bea937
   308 
   308 
   309 (** print_state **)
   309 (** print_state **)
   310 
   310 
   311 val verbose = ProofContext.verbose;
   311 val verbose = ProofContext.verbose;
   312 
   312 
       
   313 fun pretty_goals_local ctxt = Display.pretty_goals_aux
       
   314   (ProofContext.pretty_sort ctxt, ProofContext.pretty_typ ctxt, ProofContext.pretty_term ctxt);
       
   315 
   313 fun pretty_facts _ _ None = []
   316 fun pretty_facts _ _ None = []
   314   | pretty_facts s ctxt (Some ths) =
   317   | pretty_facts s ctxt (Some ths) =
   315       [Pretty.big_list (s ^ "this:") (map (ProofContext.pretty_thm ctxt) ths), Pretty.str ""];
   318       [Pretty.big_list (s ^ "this:") (map (ProofContext.pretty_thm ctxt) ths), Pretty.str ""];
   316 
   319 
   317 fun print_state nr (state as State (Node {context, facts, mode, goal = _}, nodes)) =
   320 fun print_state nr (state as State (Node {context = ctxt, facts, mode, goal = _}, nodes)) =
   318   let
   321   let
   319     val ref (_, _, begin_goal) = Display.current_goals_markers;
   322     val ref (_, _, begin_goal) = Display.current_goals_markers;
   320 
   323 
   321     fun levels_up 0 = ""
   324     fun levels_up 0 = ""
   322       | levels_up 1 = ", 1 level up"
   325       | levels_up 1 = ", 1 level up"
   325     fun subgoals 0 = ""
   328     fun subgoals 0 = ""
   326       | subgoals 1 = ", 1 subgoal"
   329       | subgoals 1 = ", 1 subgoal"
   327       | subgoals n = ", " ^ string_of_int n ^ " subgoals";
   330       | subgoals n = ", " ^ string_of_int n ^ " subgoals";
   328       
   331       
   329     fun pretty_goal (_, (i, (((kind, name, _), (goal_facts, thm)), _))) =
   332     fun pretty_goal (_, (i, (((kind, name, _), (goal_facts, thm)), _))) =
   330       pretty_facts "using " context
   333       pretty_facts "using " ctxt
   331         (if mode <> Backward orelse null goal_facts then None else Some goal_facts) @
   334         (if mode <> Backward orelse null goal_facts then None else Some goal_facts) @
   332       [Pretty.str ("goal (" ^ kind_name (sign_of state) kind ^
   335       [Pretty.str ("goal (" ^ kind_name (sign_of state) kind ^
   333           (if name = "" then "" else " " ^ name) ^
   336           (if name = "" then "" else " " ^ name) ^
   334             levels_up (i div 2) ^ subgoals (Thm.nprems_of thm) ^ "):")] @
   337             levels_up (i div 2) ^ subgoals (Thm.nprems_of thm) ^ "):")] @
   335       Display.pretty_goals_marker begin_goal (! goals_limit) thm;
   338       pretty_goals_local ctxt begin_goal (true, true) (! Display.goals_limit) thm;
   336 
   339 
   337     val ctxt_prts =
   340     val ctxt_prts =
   338       if ! verbose orelse mode = Forward then ProofContext.pretty_context context
   341       if ! verbose orelse mode = Forward then ProofContext.pretty_context ctxt
   339       else if mode = Backward then ProofContext.pretty_asms context
   342       else if mode = Backward then ProofContext.pretty_asms ctxt
   340       else [];
   343       else [];
   341 
   344 
   342     val prts =
   345     val prts =
   343      [Pretty.str ("proof (" ^ mode_name mode ^ "): step " ^ string_of_int nr ^
   346      [Pretty.str ("proof (" ^ mode_name mode ^ "): step " ^ string_of_int nr ^
   344         (if ! verbose then ", depth " ^ string_of_int (length nodes div 2 - 1)
   347         (if ! verbose then ", depth " ^ string_of_int (length nodes div 2 - 1)
   345         else "")), Pretty.str ""] @
   348         else "")), Pretty.str ""] @
   346      (if null ctxt_prts then [] else ctxt_prts @ [Pretty.str ""]) @
   349      (if null ctxt_prts then [] else ctxt_prts @ [Pretty.str ""]) @
   347      (if ! verbose orelse mode = Forward then
   350      (if ! verbose orelse mode = Forward then
   348        (pretty_facts "" context facts @ pretty_goal (find_goal state))
   351        (pretty_facts "" ctxt facts @ pretty_goal (find_goal state))
   349       else if mode = ForwardChain then pretty_facts "picking " context facts
   352       else if mode = ForwardChain then pretty_facts "picking " ctxt facts
   350       else pretty_goal (find_goal state))
   353       else pretty_goal (find_goal state))
   351   in Pretty.writeln (Pretty.chunks prts) end;
   354   in Pretty.writeln (Pretty.chunks prts) end;
   352 
   355 
   353 fun pretty_goals main_goal state =
   356 fun pretty_goals main state =
   354   let val (_, (_, ((_, (_, thm)), _))) = find_goal state
   357   let val (ctxt, (_, ((_, (_, thm)), _))) = find_goal state
   355   in Display.pretty_sub_goals main_goal (! goals_limit) thm end;
   358   in pretty_goals_local ctxt "" (false, main) (! Display.goals_limit) thm end;
   356 
   359 
   357 
   360 
   358 
   361 
   359 (** proof steps **)
   362 (** proof steps **)
   360 
   363 
   440     fun err msg = raise STATE (msg, state);
   443     fun err msg = raise STATE (msg, state);
   441 
   444 
   442     val ngoals = Thm.nprems_of raw_thm;
   445     val ngoals = Thm.nprems_of raw_thm;
   443     val _ =
   446     val _ =
   444       if ngoals = 0 then ()
   447       if ngoals = 0 then ()
   445       else (Display.print_goals ngoals raw_thm; err (string_of_int ngoals ^ " unsolved goal(s)!"));
   448       else (err (Pretty.string_of (Pretty.chunks (pretty_goals_local ctxt "" (true, true)
       
   449             (! Display.goals_limit) raw_thm @
       
   450           [Pretty.str (string_of_int ngoals ^ " unsolved goal(s)!")]))));
   446 
   451 
   447     val thm = raw_thm RS Drule.rev_triv_goal;
   452     val thm = raw_thm RS Drule.rev_triv_goal;
   448     val {hyps, prop, sign, ...} = Thm.rep_thm thm;
   453     val {hyps, prop, sign, ...} = Thm.rep_thm thm;
   449     val tsig = Sign.tsig_of sign;
   454     val tsig = Sign.tsig_of sign;
   450 
   455 
   711   |> (Seq.flat o Seq.map (finish_local print));
   716   |> (Seq.flat o Seq.map (finish_local print));
   712 
   717 
   713 
   718 
   714 (* global_qed *)
   719 (* global_qed *)
   715 
   720 
       
   721 fun locale_prefix None f thy = f thy
       
   722   | locale_prefix (Some (loc, _)) f thy = thy |> Theory.add_path loc |> f |>> Theory.parent_path;
       
   723 
   716 fun locale_store_thm _ None _ = I
   724 fun locale_store_thm _ None _ = I
   717   | locale_store_thm name (Some (loc, atts)) th = Locale.store_thm loc ((name, th), atts);
   725   | locale_store_thm name (Some (loc, atts)) th = Locale.store_thm loc ((name, th), atts);
   718 
   726 
   719 fun finish_global state =
   727 fun finish_global state =
   720   let
   728   let
   733     val (kname, atts, locale) =
   741     val (kname, atts, locale) =
   734       (case kind of Theorem (s, locale, atts) => (s, atts @ [Drule.kind s], locale)
   742       (case kind of Theorem (s, locale, atts) => (s, atts @ [Drule.kind s], locale)
   735       | _ => err_malformed "finish_global" state);
   743       | _ => err_malformed "finish_global" state);
   736     val (thy', result') =
   744     val (thy', result') =
   737       theory_of state
   745       theory_of state
   738       |> PureThy.store_thm ((name, result), atts)
   746       |> locale_prefix locale (PureThy.store_thm ((name, result), atts))
   739       |>> locale_store_thm name locale locale_result;
   747       |>> locale_store_thm name locale locale_result;
   740   in (thy', {kind = kname, name = name, thm = result'}) end;
   748   in (thy', {kind = kname, name = name, thm = result'}) end;
   741 
   749 
   742 (*note: clients should inspect first result only, as backtracking may destroy theory*)
   750 (*note: clients should inspect first result only, as backtracking may destroy theory*)
   743 fun global_qed finalize state =
   751 fun global_qed finalize state =