src/Pure/goal.ML
author wenzelm
Sat Apr 12 17:00:40 2008 +0200 (2008-04-12)
changeset 26628 63306cb94313
parent 25301 24e027f55f45
child 26713 1c6181def1d0
permissions -rw-r--r--
replaced Drule.close_derivation/Goal.close_result by Thm.close_derivation (removed obsolete compression);
     1 (*  Title:      Pure/goal.ML
     2     ID:         $Id$
     3     Author:     Makarius and Lawrence C Paulson
     4 
     5 Goals in tactical theorem proving.
     6 *)
     7 
     8 signature BASIC_GOAL =
     9 sig
    10   val SELECT_GOAL: tactic -> int -> tactic
    11   val CONJUNCTS: tactic -> int -> tactic
    12   val PRECISE_CONJUNCTS: int -> tactic -> int -> tactic
    13 end;
    14 
    15 signature GOAL =
    16 sig
    17   include BASIC_GOAL
    18   val init: cterm -> thm
    19   val protect: thm -> thm
    20   val conclude: thm -> thm
    21   val finish: thm -> thm
    22   val norm_result: thm -> thm
    23   val prove_internal: cterm list -> cterm -> (thm list -> tactic) -> thm
    24   val prove_multi: Proof.context -> string list -> term list -> term list ->
    25     ({prems: thm list, context: Proof.context} -> tactic) -> thm list
    26   val prove: Proof.context -> string list -> term list -> term ->
    27     ({prems: thm list, context: Proof.context} -> tactic) -> thm
    28   val prove_global: theory -> string list -> term list -> term -> (thm list -> tactic) -> thm
    29   val extract: int -> int -> thm -> thm Seq.seq
    30   val retrofit: int -> int -> thm -> thm -> thm Seq.seq
    31   val conjunction_tac: int -> tactic
    32   val precise_conjunction_tac: int -> int -> tactic
    33   val recover_conjunction_tac: tactic
    34   val norm_hhf_tac: int -> tactic
    35   val compose_hhf_tac: thm -> int -> tactic
    36   val assume_rule_tac: Proof.context -> int -> tactic
    37 end;
    38 
    39 structure Goal: GOAL =
    40 struct
    41 
    42 (** goals **)
    43 
    44 (*
    45   -------- (init)
    46   C ==> #C
    47 *)
    48 val init =
    49   let val A = #1 (Thm.dest_implies (Thm.cprop_of Drule.protectI))
    50   in fn C => Thm.instantiate ([], [(A, C)]) Drule.protectI end;
    51 
    52 (*
    53    C
    54   --- (protect)
    55   #C
    56 *)
    57 fun protect th = th COMP_INCR Drule.protectI;
    58 
    59 (*
    60   A ==> ... ==> #C
    61   ---------------- (conclude)
    62   A ==> ... ==> C
    63 *)
    64 fun conclude th =
    65   (case SINGLE (Thm.compose_no_flatten false (th, Thm.nprems_of th) 1)
    66       (Drule.incr_indexes th Drule.protectD) of
    67     SOME th' => th'
    68   | NONE => raise THM ("Failed to conclude goal", 0, [th]));
    69 
    70 (*
    71   #C
    72   --- (finish)
    73    C
    74 *)
    75 fun finish th =
    76   (case Thm.nprems_of th of
    77     0 => conclude th
    78   | n => raise THM ("Proof failed.\n" ^
    79       Pretty.string_of (Pretty.chunks (Display.pretty_goals n th)) ^
    80       ("\n" ^ string_of_int n ^ " unsolved goal(s)!"), 0, [th]));
    81 
    82 
    83 
    84 (** results **)
    85 
    86 val norm_result =
    87   Drule.flexflex_unique
    88   #> MetaSimplifier.norm_hhf_protect
    89   #> Thm.strip_shyps
    90   #> Drule.zero_var_indexes;
    91 
    92 
    93 
    94 (** tactical theorem proving **)
    95 
    96 (* prove_internal -- minimal checks, no normalization of result! *)
    97 
    98 fun prove_internal casms cprop tac =
    99   (case SINGLE (tac (map Assumption.assume casms)) (init cprop) of
   100     SOME th => Drule.implies_intr_list casms (finish th)
   101   | NONE => error "Tactic failed.");
   102 
   103 
   104 (* prove_multi *)
   105 
   106 fun prove_multi ctxt xs asms props tac =
   107   let
   108     val thy = ProofContext.theory_of ctxt;
   109     val string_of_term = Sign.string_of_term thy;
   110 
   111     fun err msg = cat_error msg
   112       ("The error(s) above occurred for the goal statement:\n" ^
   113         string_of_term (Logic.list_implies (asms, Logic.mk_conjunction_list props)));
   114 
   115     fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm (Term.no_dummy_patterns t))
   116       handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
   117     val casms = map cert_safe asms;
   118     val cprops = map cert_safe props;
   119 
   120     val (prems, ctxt') = ctxt
   121       |> Variable.add_fixes_direct xs
   122       |> fold Variable.declare_internal (asms @ props)
   123       |> Assumption.add_assumes casms;
   124 
   125     val goal = init (Conjunction.mk_conjunction_balanced cprops);
   126     val res =
   127       (case SINGLE (tac {prems = prems, context = ctxt'}) goal of
   128         NONE => err "Tactic failed."
   129       | SOME res => res);
   130     val results = Conjunction.elim_balanced (length props) (finish res)
   131       handle THM (msg, _, _) => err msg;
   132     val _ = Unify.matches_list thy (map Thm.term_of cprops) (map Thm.prop_of results)
   133       orelse err ("Proved a different theorem: " ^ string_of_term (Thm.prop_of res));
   134   in
   135     results
   136     |> map (Assumption.export false ctxt' ctxt)
   137     |> Variable.export ctxt' ctxt
   138     |> map Drule.zero_var_indexes
   139   end;
   140 
   141 
   142 (* prove *)
   143 
   144 fun prove ctxt xs asms prop tac = hd (prove_multi ctxt xs asms [prop] tac);
   145 
   146 fun prove_global thy xs asms prop tac =
   147   Drule.standard (prove (ProofContext.init thy) xs asms prop (fn {prems, ...} => tac prems));
   148 
   149 
   150 
   151 (** goal structure **)
   152 
   153 (* nested goals *)
   154 
   155 fun extract i n st =
   156   (if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st then Seq.empty
   157    else if n = 1 then Seq.single (Thm.cprem_of st i)
   158    else
   159      Seq.single (Conjunction.mk_conjunction_balanced (map (Thm.cprem_of st) (i upto i + n - 1))))
   160   |> Seq.map (Thm.adjust_maxidx_cterm ~1 #> init);
   161 
   162 fun retrofit i n st' st =
   163   (if n = 1 then st
   164    else st |> Drule.with_subgoal i (Conjunction.uncurry_balanced n))
   165   |> Thm.compose_no_flatten false (conclude st', Thm.nprems_of st') i;
   166 
   167 fun SELECT_GOAL tac i st =
   168   if Thm.nprems_of st = 1 andalso i = 1 then tac st
   169   else Seq.lifts (retrofit i 1) (Seq.maps tac (extract i 1 st)) st;
   170 
   171 
   172 (* multiple goals *)
   173 
   174 fun precise_conjunction_tac 0 i = eq_assume_tac i
   175   | precise_conjunction_tac 1 i = SUBGOAL (K all_tac) i
   176   | precise_conjunction_tac n i = PRIMITIVE (Drule.with_subgoal i (Conjunction.curry_balanced n));
   177 
   178 val adhoc_conjunction_tac = REPEAT_ALL_NEW
   179   (SUBGOAL (fn (goal, i) =>
   180     if can Logic.dest_conjunction goal then rtac Conjunction.conjunctionI i
   181     else no_tac));
   182 
   183 val conjunction_tac = SUBGOAL (fn (goal, i) =>
   184   precise_conjunction_tac (length (Logic.dest_conjunctions goal)) i ORELSE
   185   TRY (adhoc_conjunction_tac i));
   186 
   187 val recover_conjunction_tac = PRIMITIVE (fn th =>
   188   Conjunction.uncurry_balanced (Thm.nprems_of th) th);
   189 
   190 fun PRECISE_CONJUNCTS n tac =
   191   SELECT_GOAL (precise_conjunction_tac n 1
   192     THEN tac
   193     THEN recover_conjunction_tac);
   194 
   195 fun CONJUNCTS tac =
   196   SELECT_GOAL (conjunction_tac 1
   197     THEN tac
   198     THEN recover_conjunction_tac);
   199 
   200 
   201 (* hhf normal form *)
   202 
   203 val norm_hhf_tac =
   204   rtac Drule.asm_rl  (*cheap approximation -- thanks to builtin Logic.flatten_params*)
   205   THEN' SUBGOAL (fn (t, i) =>
   206     if Drule.is_norm_hhf t then all_tac
   207     else MetaSimplifier.rewrite_goal_tac [Drule.norm_hhf_eq] i);
   208 
   209 fun compose_hhf_tac th i st =
   210   PRIMSEQ (Thm.bicompose false (false, Drule.lift_all (Thm.cprem_of st i) th, 0) i) st;
   211 
   212 
   213 (* non-atomic goal assumptions *)
   214 
   215 fun non_atomic (Const ("==>", _) $ _ $ _) = true
   216   | non_atomic (Const ("all", _) $ _) = true
   217   | non_atomic _ = false;
   218 
   219 fun assume_rule_tac ctxt = norm_hhf_tac THEN' CSUBGOAL (fn (goal, i) =>
   220   let
   221     val ((_, goal'), ctxt') = Variable.focus goal ctxt;
   222     val goal'' = Drule.cterm_rule (singleton (Variable.export ctxt' ctxt)) goal';
   223     val Rs = filter (non_atomic o Thm.term_of) (Drule.strip_imp_prems goal'');
   224     val tacs = Rs |> map (fn R =>
   225       Tactic.etac (MetaSimplifier.norm_hhf (Thm.trivial R)) THEN_ALL_NEW assume_tac);
   226   in fold_rev (curry op APPEND') tacs (K no_tac) i end);
   227 
   228 end;
   229 
   230 structure BasicGoal: BASIC_GOAL = Goal;
   231 open BasicGoal;