src/Pure/goal.ML
changeset 17980 788836292b1a
child 17983 89103008502f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/goal.ML	Fri Oct 21 18:15:00 2005 +0200
     1.3 @@ -0,0 +1,164 @@
     1.4 +(*  Title:      Pure/goal.ML
     1.5 +    ID:         $Id$
     1.6 +    Author:     Makarius and Lawrence C Paulson
     1.7 +
     1.8 +Internal goals.  NB: by attaching the Goal constant the conclusion of
     1.9 +a goal state is guaranteed to be atomic.
    1.10 +*)
    1.11 +
    1.12 +signature BASIC_GOAL =
    1.13 +sig
    1.14 +  val SELECT_GOAL: tactic -> int -> tactic
    1.15 +end;
    1.16 +
    1.17 +signature GOAL =
    1.18 +sig
    1.19 +  include BASIC_GOAL
    1.20 +  val init: cterm -> thm
    1.21 +  val conclude: thm -> thm
    1.22 +  val finish: thm -> thm
    1.23 +  val prove_raw: theory -> term -> tactic -> thm
    1.24 +  val norm_hhf_rule: thm -> thm
    1.25 +  val prove: theory -> string list -> term list -> term -> (thm list -> tactic) -> thm
    1.26 +  val prove_multi: theory -> string list -> term list -> term list ->
    1.27 +    (thm list -> tactic) -> thm list
    1.28 +
    1.29 +  (* FIXME remove *)
    1.30 +  val norm_hhf_plain: thm -> thm
    1.31 +  val prove_multi_plain: theory -> string list -> term list -> term list ->
    1.32 +    (thm list -> tactic) -> thm list
    1.33 +  val prove_plain: theory -> string list -> term list -> term -> (thm list -> tactic) -> thm
    1.34 +end;
    1.35 +
    1.36 +structure Goal: GOAL =
    1.37 +struct
    1.38 +
    1.39 +(* managing goal states *)
    1.40 +
    1.41 +(*
    1.42 +  ----------------- (init)
    1.43 +  Goal C ==> Goal C
    1.44 +*)
    1.45 +fun init ct = Drule.instantiate' [] [SOME ct] Drule.goalI;
    1.46 +
    1.47 +(*
    1.48 +  A ==> ... ==> Goal C
    1.49 +  -------------------- (conclude)
    1.50 +  A ==> ... ==> C
    1.51 +*)
    1.52 +fun conclude th =
    1.53 +  (case SINGLE (Thm.bicompose false (false, th, Thm.nprems_of th) 1)
    1.54 +      (Drule.incr_indexes_wrt [] [] [] [th] Drule.goalD) of
    1.55 +    SOME th' => th'
    1.56 +  | NONE => raise THM ("Failed to conclude goal", 0, [th]));
    1.57 +
    1.58 +(*
    1.59 +  Goal C
    1.60 +  ------ (finish)
    1.61 +    C
    1.62 +*)  
    1.63 +fun finish th =
    1.64 +  (case Thm.nprems_of th of
    1.65 +    0 => conclude th
    1.66 +  | n => raise THM ("Proof failed.\n" ^
    1.67 +      Pretty.string_of (Pretty.chunks (Display.pretty_goals n th)) ^
    1.68 +      ("\n" ^ string_of_int n ^ " unsolved goal(s)!"), 0, [th]));
    1.69 +
    1.70 +
    1.71 +(* prove_raw -- minimal checks, no normalization *)
    1.72 +
    1.73 +fun prove_raw thy goal tac =  
    1.74 +  (case SINGLE tac (init (Thm.cterm_of thy goal)) of
    1.75 +    SOME th => finish th
    1.76 +  | NONE => raise ERROR_MESSAGE "Tactic failed.");
    1.77 +
    1.78 +
    1.79 +(* tactical proving *)
    1.80 +
    1.81 +val norm_hhf_plain =  (* FIXME remove *)
    1.82 +  (not o Drule.is_norm_hhf o Thm.prop_of) ?
    1.83 +    MetaSimplifier.simplify_aux (K (K NONE)) true [Drule.norm_hhf_eq];
    1.84 +
    1.85 +val norm_hhf_rule =
    1.86 +  norm_hhf_plain
    1.87 +  #> Thm.adjust_maxidx_thm
    1.88 +  #> Drule.gen_all;
    1.89 +
    1.90 +local
    1.91 +
    1.92 +fun gen_prove finish_thm thy xs asms props tac =
    1.93 +  let
    1.94 +    val prop = Logic.mk_conjunction_list props;
    1.95 +    val statement = Logic.list_implies (asms, prop);
    1.96 +    val frees = map Term.dest_Free (Term.term_frees statement);
    1.97 +    val fixed_frees = filter_out (member (op =) xs o #1) frees;
    1.98 +    val fixed_tfrees = foldr Term.add_typ_tfrees [] (map #2 fixed_frees);
    1.99 +    val params = List.mapPartial (fn x => Option.map (pair x) (AList.lookup (op =) frees x)) xs;
   1.100 +
   1.101 +    fun err msg = raise ERROR_MESSAGE
   1.102 +      (msg ^ "\nThe error(s) above occurred for the goal statement:\n" ^
   1.103 +        Sign.string_of_term thy (Term.list_all_free (params, statement)));
   1.104 +
   1.105 +    fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm t)
   1.106 +      handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
   1.107 +
   1.108 +    val _ = cert_safe statement;
   1.109 +    val _ = Term.no_dummy_patterns statement handle TERM (msg, _) => err msg;
   1.110 +
   1.111 +    val cparams = map (cert_safe o Free) params;
   1.112 +    val casms = map cert_safe asms;
   1.113 +    val prems = map (norm_hhf_rule o Thm.assume) casms;
   1.114 +
   1.115 +    val goal = init (cert_safe prop);
   1.116 +    val goal' = (case SINGLE (tac prems) goal of SOME goal' => goal' | _ => err "Tactic failed.");
   1.117 +    val raw_result = finish goal' handle THM (msg, _, _) => err msg;
   1.118 +
   1.119 +    val prop' = Thm.prop_of raw_result;
   1.120 +    val _ = conditional (not (Pattern.matches thy (prop, prop'))) (fn () =>
   1.121 +      err ("Proved a different theorem: " ^ Sign.string_of_term thy prop'));
   1.122 +  in
   1.123 +    Drule.conj_elim_precise (length props) raw_result
   1.124 +    |> map
   1.125 +      (Drule.implies_intr_list casms
   1.126 +        #> Drule.forall_intr_list cparams
   1.127 +        #> finish_thm fixed_tfrees)
   1.128 +  end;
   1.129 +
   1.130 +in
   1.131 +
   1.132 +fun prove_multi thy xs asms prop tac =
   1.133 +  gen_prove (fn fixed_tfrees => Drule.zero_var_indexes o
   1.134 +      (#1 o Thm.varifyT' fixed_tfrees) o norm_hhf_rule)
   1.135 +    thy xs asms prop tac;
   1.136 +
   1.137 +fun prove thy xs asms prop tac = hd (prove_multi thy xs asms [prop] tac);
   1.138 +
   1.139 +fun prove_multi_plain thy xs asms prop tac = gen_prove (K norm_hhf_plain) thy xs asms prop tac;
   1.140 +fun prove_plain thy xs asms prop tac = hd (prove_multi_plain thy xs asms [prop] tac);
   1.141 +
   1.142 +end;
   1.143 +
   1.144 +
   1.145 +(* SELECT_GOAL *)
   1.146 +
   1.147 +(*Tactical for restricting the effect of a tactic to subgoal i.  Works
   1.148 +  by making a new state from subgoal i, applying tac to it, and
   1.149 +  composing the resulting thm with the original state.*)
   1.150 +
   1.151 +fun SELECT tac i st =
   1.152 +  init (Thm.adjust_maxidx (List.nth (Drule.cprems_of st, i - 1)))
   1.153 +  |> tac
   1.154 +  |> Seq.maps (fn st' => Thm.bicompose false (false, conclude st', Thm.nprems_of st') i st);
   1.155 +
   1.156 +fun SELECT_GOAL tac i st =
   1.157 +  let val n = Thm.nprems_of st in
   1.158 +    if 1 <= i andalso i <= n then
   1.159 +      if n = 1 then tac st else SELECT tac i st
   1.160 +    else Seq.empty
   1.161 +  end;
   1.162 +
   1.163 +
   1.164 +end;
   1.165 +
   1.166 +structure BasicGoal: BASIC_GOAL = Goal;
   1.167 +open BasicGoal;