src/Pure/Isar/proof.ML
changeset 11891 99569e5f0ab5
parent 11816 545aab7410ac
child 11906 a71713885e3e
equal deleted inserted replaced
11890:28e42a90bea8 11891:99569e5f0ab5
    76   val presume:
    76   val presume:
    77     ((string * context attribute list) * (string * (string list * string list)) list) list
    77     ((string * context attribute list) * (string * (string list * string list)) list) list
    78     -> state -> state
    78     -> state -> state
    79   val presume_i: ((string * context attribute list) * (term * (term list * term list)) list) list
    79   val presume_i: ((string * context attribute list) * (term * (term list * term list)) list) list
    80     -> state -> state
    80     -> state -> state
       
    81   val def: string -> context attribute list -> string *  (string * string list) -> state -> state
       
    82   val def_i: string -> context attribute list -> string * (term * term list) -> state -> state
    81   val invoke_case: string * string option list * context attribute list -> state -> state
    83   val invoke_case: string * string option list * context attribute list -> state -> state
    82   val theorem: string -> bstring -> theory attribute list -> string * (string list * string list)
    84   val theorem: string -> bstring -> theory attribute list -> string * (string list * string list)
    83     -> theory -> state
    85     -> theory -> state
    84   val theorem_i: string -> bstring -> theory attribute list -> term * (term list * term list)
    86   val theorem_i: string -> bstring -> theory attribute list -> term * (term list * term list)
    85     -> theory -> state
    87     -> theory -> state
   317   | pretty_facts s (Some ths) =
   319   | pretty_facts s (Some ths) =
   318       [Pretty.big_list (s ^ "this:") (map pretty_thm ths), Pretty.str ""];
   320       [Pretty.big_list (s ^ "this:") (map pretty_thm ths), Pretty.str ""];
   319 
   321 
   320 fun print_state nr (state as State (Node {context, facts, mode, goal = _}, nodes)) =
   322 fun print_state nr (state as State (Node {context, facts, mode, goal = _}, nodes)) =
   321   let
   323   let
   322     val ref (_, _, begin_goal) = Goals.current_goals_markers;
   324     val ref (_, _, begin_goal) = Display.current_goals_markers;
   323 
   325 
   324     fun levels_up 0 = ""
   326     fun levels_up 0 = ""
   325       | levels_up 1 = ", 1 level up"
   327       | levels_up 1 = ", 1 level up"
   326       | levels_up i = ", " ^ string_of_int i ^ " levels up";
   328       | levels_up i = ", " ^ string_of_int i ^ " levels up";
   327 
   329 
   333       pretty_facts "using "
   335       pretty_facts "using "
   334         (if mode <> Backward orelse null goal_facts then None else Some goal_facts) @
   336         (if mode <> Backward orelse null goal_facts then None else Some goal_facts) @
   335       [Pretty.str ("goal (" ^ kind_name kind ^
   337       [Pretty.str ("goal (" ^ kind_name kind ^
   336           (if name = "" then "" else " " ^ name) ^
   338           (if name = "" then "" else " " ^ name) ^
   337             levels_up (i div 2) ^ subgoals (Thm.nprems_of thm) ^ "):")] @
   339             levels_up (i div 2) ^ subgoals (Thm.nprems_of thm) ^ "):")] @
   338       Locale.pretty_goals_marker begin_goal (! goals_limit) thm;
   340       Display.pretty_goals_marker begin_goal (! goals_limit) thm;
   339 
   341 
   340     val ctxt_prts =
   342     val ctxt_prts =
   341       if ! verbose orelse mode = Forward then ProofContext.pretty_context context
   343       if ! verbose orelse mode = Forward then ProofContext.pretty_context context
   342       else if mode = Backward then ProofContext.pretty_prems context
   344       else if mode = Backward then ProofContext.pretty_prems context
   343       else [];
   345       else [];
   353       else pretty_goal (find_goal state))
   355       else pretty_goal (find_goal state))
   354   in Pretty.writeln (Pretty.chunks prts) end;
   356   in Pretty.writeln (Pretty.chunks prts) end;
   355 
   357 
   356 fun pretty_goals main_goal state =
   358 fun pretty_goals main_goal state =
   357   let val (_, (_, ((_, (_, thm)), _))) = find_goal state
   359   let val (_, (_, ((_, (_, thm)), _))) = find_goal state
   358   in Locale.pretty_sub_goals main_goal (! goals_limit) thm end;
   360   in Display.pretty_sub_goals main_goal (! goals_limit) thm end;
   359 
   361 
   360 
   362 
   361 
   363 
   362 (** proof steps **)
   364 (** proof steps **)
   363 
   365 
   455     fun err msg = raise STATE (msg, state);
   457     fun err msg = raise STATE (msg, state);
   456 
   458 
   457     val ngoals = Thm.nprems_of raw_thm;
   459     val ngoals = Thm.nprems_of raw_thm;
   458     val _ =
   460     val _ =
   459       if ngoals = 0 then ()
   461       if ngoals = 0 then ()
   460       else (Locale.print_goals ngoals raw_thm; err (string_of_int ngoals ^ " unsolved goal(s)!"));
   462       else (Display.print_goals ngoals raw_thm; err (string_of_int ngoals ^ " unsolved goal(s)!"));
   461 
   463 
   462     val thm = raw_thm RS Drule.rev_triv_goal;
   464     val thm = raw_thm RS Drule.rev_triv_goal;
   463     val {hyps, prop, sign, ...} = Thm.rep_thm thm;
   465     val {hyps, prop, sign, ...} = Thm.rep_thm thm;
   464     val tsig = Sign.tsig_of sign;
   466     val tsig = Sign.tsig_of sign;
   465 
   467 
   518 
   520 
   519 val fix = gen_fix ProofContext.fix;
   521 val fix = gen_fix ProofContext.fix;
   520 val fix_i = gen_fix ProofContext.fix_i;
   522 val fix_i = gen_fix ProofContext.fix_i;
   521 
   523 
   522 
   524 
   523 (* assume *)
   525 (* assume and presume *)
   524 
   526 
   525 local
   527 local
   526 
   528 
   527 fun gen_assume f exp args state =
   529 fun gen_assume f exp args state =
   528   state
   530   state
   543 val assm_i = gen_assume ProofContext.assume_i;
   545 val assm_i = gen_assume ProofContext.assume_i;
   544 val assume = assm export_assume;
   546 val assume = assm export_assume;
   545 val assume_i = assm_i export_assume;
   547 val assume_i = assm_i export_assume;
   546 val presume = assm export_presume;
   548 val presume = assm export_presume;
   547 val presume_i = assm_i export_presume;
   549 val presume_i = assm_i export_presume;
       
   550 
       
   551 end;
       
   552 
       
   553 
       
   554 (** local definitions **)
       
   555 
       
   556 local
       
   557 
       
   558 fun dest_lhs cprop =
       
   559   let val x = #1 (Logic.dest_equals (Thm.term_of cprop))
       
   560   in Term.dest_Free x; Thm.cterm_of (Thm.sign_of_cterm cprop) x end
       
   561   handle TERM _ => raise TERM ("Malformed definitional assumption encountered:\n" ^
       
   562       quote (Display.string_of_cterm cprop), []);
       
   563 
       
   564 fun export_def _ cprops thm =
       
   565   thm
       
   566   |> Drule.implies_intr_list cprops
       
   567   |> Drule.forall_intr_list (map dest_lhs cprops)
       
   568   |> Drule.forall_elim_vars 0
       
   569   |> RANGE (replicate (length cprops) (Tactic.rtac Drule.reflexive_thm)) 1;
       
   570 
       
   571 fun gen_def fix prep_term prep_pats raw_name atts (x, (raw_rhs, raw_pats)) state =
       
   572   let
       
   573     val _ = assert_forward state;
       
   574     val ctxt = context_of state;
       
   575 
       
   576     (*rhs*)
       
   577     val name = if raw_name = "" then Thm.def_name x else raw_name;
       
   578     val rhs = prep_term ctxt raw_rhs;
       
   579     val T = Term.fastype_of rhs;
       
   580     val pats = prep_pats T (ProofContext.declare_term rhs ctxt) raw_pats;
       
   581 
       
   582     (*lhs*)
       
   583     val lhs = ProofContext.bind_skolem ctxt [x] (Free (x, T));
       
   584   in
       
   585     state
       
   586     |> fix [([x], None)]
       
   587     |> match_bind_i [(pats, rhs)]
       
   588     |> assm_i export_def [((name, atts), [(Logic.mk_equals (lhs, rhs), ([], []))])]
       
   589   end;
       
   590 
       
   591 in
       
   592 
       
   593 val def = gen_def fix ProofContext.read_term ProofContext.read_term_pats;
       
   594 val def_i = gen_def fix_i ProofContext.cert_term ProofContext.cert_term_pats;
   548 
   595 
   549 end;
   596 end;
   550 
   597 
   551 
   598 
   552 (* invoke_case *)
   599 (* invoke_case *)