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