src/Pure/goal.ML
author wenzelm
Mon Jun 18 23:30:46 2007 +0200 (2007-06-18)
changeset 23414 927203ad4b3a
parent 23356 dbe3731241c3
child 23418 c195f6f13769
permissions -rw-r--r--
tuned conjunction tactics: slightly smaller proof terms;
     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 PRECISE_CONJUNCTS: int -> tactic -> int -> tactic
    12   val CONJUNCTS: 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 precise_conjunction_tac: int -> int -> tactic
    33   val conjunction_tac: int -> tactic
    34   val asm_rewrite_goal_tac: bool * bool * bool -> (simpset -> tactic) -> simpset -> int -> tactic
    35   val rewrite_goal_tac: thm list -> int -> tactic
    36   val norm_hhf_tac: int -> tactic
    37   val compose_hhf: thm -> int -> thm -> thm Seq.seq
    38   val compose_hhf_tac: thm -> int -> tactic
    39   val comp_hhf: thm -> thm -> thm
    40   val assume_rule_tac: Proof.context -> int -> tactic
    41 end;
    42 
    43 structure Goal: GOAL =
    44 struct
    45 
    46 (** goals **)
    47 
    48 (*
    49   -------- (init)
    50   C ==> #C
    51 *)
    52 val init =
    53   let val A = #1 (Thm.dest_implies (Thm.cprop_of Drule.protectI))
    54   in fn C => Thm.instantiate ([], [(A, C)]) Drule.protectI end;
    55 
    56 (*
    57    C
    58   --- (protect)
    59   #C
    60 *)
    61 fun protect th = th COMP_INCR Drule.protectI;
    62 
    63 (*
    64   A ==> ... ==> #C
    65   ---------------- (conclude)
    66   A ==> ... ==> C
    67 *)
    68 fun conclude th =
    69   (case SINGLE (Thm.compose_no_flatten false (th, Thm.nprems_of th) 1)
    70       (Drule.incr_indexes th Drule.protectD) of
    71     SOME th' => th'
    72   | NONE => raise THM ("Failed to conclude goal", 0, [th]));
    73 
    74 (*
    75   #C
    76   --- (finish)
    77    C
    78 *)
    79 fun finish th =
    80   (case Thm.nprems_of th of
    81     0 => conclude th
    82   | n => raise THM ("Proof failed.\n" ^
    83       Pretty.string_of (Pretty.chunks (Display.pretty_goals n th)) ^
    84       ("\n" ^ string_of_int n ^ " unsolved goal(s)!"), 0, [th]));
    85 
    86 
    87 
    88 (** results **)
    89 
    90 (* normal form *)
    91 
    92 val norm_result =
    93   Drule.flexflex_unique
    94   #> MetaSimplifier.norm_hhf_protect
    95   #> Thm.strip_shyps
    96   #> Drule.zero_var_indexes;
    97 
    98 val close_result =
    99   Thm.compress
   100   #> Drule.close_derivation;
   101 
   102 
   103 
   104 (** tactical theorem proving **)
   105 
   106 (* prove_internal -- minimal checks, no normalization of result! *)
   107 
   108 fun prove_internal casms cprop tac =
   109   (case SINGLE (tac (map Assumption.assume casms)) (init cprop) of
   110     SOME th => Drule.implies_intr_list casms (finish th)
   111   | NONE => error "Tactic failed.");
   112 
   113 
   114 (* prove_multi *)
   115 
   116 fun prove_multi ctxt xs asms props tac =
   117   let
   118     val thy = ProofContext.theory_of ctxt;
   119     val string_of_term = Sign.string_of_term thy;
   120 
   121     fun err msg = cat_error msg
   122       ("The error(s) above occurred for the goal statement:\n" ^
   123         string_of_term (Logic.list_implies (asms, Logic.mk_conjunction_list props)));
   124 
   125     fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm (Term.no_dummy_patterns t))
   126       handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
   127     val casms = map cert_safe asms;
   128     val cprops = map cert_safe props;
   129 
   130     val (prems, ctxt') = ctxt
   131       |> Variable.add_fixes_direct xs
   132       |> fold Variable.declare_internal (asms @ props)
   133       |> Assumption.add_assumes casms;
   134 
   135     val goal = init (Conjunction.mk_conjunction_list cprops);
   136     val res =
   137       (case SINGLE (tac {prems = prems, context = ctxt'}) goal of
   138         NONE => err "Tactic failed."
   139       | SOME res => res);
   140     val [results] = Conjunction.elim_precise [length props] (finish res)
   141       handle THM (msg, _, _) => err msg;
   142     val _ = Unify.matches_list thy (map Thm.term_of cprops) (map Thm.prop_of results)
   143       orelse err ("Proved a different theorem: " ^ string_of_term (Thm.prop_of res));
   144   in
   145     results
   146     |> map (Assumption.export false ctxt' ctxt)
   147     |> Variable.export ctxt' ctxt
   148     |> map Drule.zero_var_indexes
   149   end;
   150 
   151 
   152 (* prove *)
   153 
   154 fun prove ctxt xs asms prop tac = hd (prove_multi ctxt xs asms [prop] tac);
   155 
   156 fun prove_global thy xs asms prop tac =
   157   Drule.standard (prove (ProofContext.init thy) xs asms prop (fn {prems, ...} => tac prems));
   158 
   159 
   160 
   161 (** goal structure **)
   162 
   163 (* nested goals *)
   164 
   165 fun extract i n st =
   166   (if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st then Seq.empty
   167    else if n = 1 then Seq.single (Thm.cprem_of st i)
   168    else Seq.single (foldr1 Conjunction.mk_conjunction (map (Thm.cprem_of st) (i upto i + n - 1))))
   169   |> Seq.map (Thm.adjust_maxidx_cterm ~1 #> init);
   170 
   171 fun retrofit i n st' st =
   172   (if n = 1 then st
   173    else st |> Drule.rotate_prems (i - 1) |> Conjunction.uncurry n |> Drule.rotate_prems (1 - i))
   174   |> Thm.compose_no_flatten false (conclude st', Thm.nprems_of st') i;
   175 
   176 fun SELECT_GOAL tac i st =
   177   if Thm.nprems_of st = 1 andalso i = 1 then tac st
   178   else Seq.lifts (retrofit i 1) (Seq.maps tac (extract i 1 st)) st;
   179 
   180 
   181 (* multiple goals *)
   182 
   183 local
   184 
   185 fun conj_intrs n =
   186   let
   187     val cert = Thm.cterm_of ProtoPure.thy;
   188     val names = Name.invents Name.context "A" n;
   189     val As = map (fn name => cert (Free (name, propT))) names;
   190   in
   191     Thm.generalize ([], names) 0
   192       (Drule.implies_intr_list As (Conjunction.intr_list (map Thm.assume As)))
   193   end;
   194 
   195 fun count_conjs A =
   196   (case try Logic.dest_conjunction A of
   197     NONE => 1
   198   | SOME (_, B) => count_conjs B + 1);
   199 
   200 in
   201 
   202 val precise_conjunction_tac =
   203   let
   204     fun tac 0 i = eq_assume_tac i
   205       | tac 1 i = SUBGOAL (K all_tac) i
   206       | tac 2 i = rtac Conjunction.conjunctionI i
   207       | tac n i = rtac (conj_intrs n) i;
   208   in TRY oo tac end;
   209 
   210 val conjunction_tac = TRY o REPEAT_ALL_NEW (SUBGOAL (fn (goal, i) =>
   211   let val n = count_conjs goal
   212   in if n < 2 then no_tac else precise_conjunction_tac n i end));
   213 
   214 fun PRECISE_CONJUNCTS n tac =
   215   SELECT_GOAL (precise_conjunction_tac n 1
   216     THEN tac
   217     THEN PRIMITIVE (Conjunction.uncurry ~1));
   218 
   219 fun CONJUNCTS tac =
   220   SELECT_GOAL (conjunction_tac 1
   221     THEN tac
   222     THEN PRIMITIVE (Conjunction.uncurry ~1));
   223 
   224 end;
   225 
   226 
   227 (* rewriting *)
   228 
   229 (*Rewrite subgoal i only.  SELECT_GOAL avoids inefficiencies in goals_conv.*)
   230 fun asm_rewrite_goal_tac mode prover_tac ss =
   231   SELECT_GOAL
   232     (PRIMITIVE (MetaSimplifier.rewrite_goal_rule mode (SINGLE o prover_tac) ss 1));
   233 
   234 fun rewrite_goal_tac rews =
   235   let val ss = MetaSimplifier.empty_ss addsimps rews in
   236     fn i => fn st => asm_rewrite_goal_tac (true, false, false) (K no_tac)
   237       (MetaSimplifier.theory_context (Thm.theory_of_thm st) ss) i st
   238   end;
   239 
   240 
   241 (* hhf normal form *)
   242 
   243 val norm_hhf_tac =
   244   rtac Drule.asm_rl  (*cheap approximation -- thanks to builtin Logic.flatten_params*)
   245   THEN' SUBGOAL (fn (t, i) =>
   246     if Drule.is_norm_hhf t then all_tac
   247     else rewrite_goal_tac [Drule.norm_hhf_eq] i);
   248 
   249 fun compose_hhf tha i thb =
   250   Thm.bicompose false (false, Drule.lift_all (Thm.cprem_of thb i) tha, 0) i thb;
   251 
   252 fun compose_hhf_tac th i = PRIMSEQ (compose_hhf th i);
   253 
   254 fun comp_hhf tha thb =
   255   (case Seq.chop 2 (compose_hhf tha 1 thb) of
   256     ([th], _) => th
   257   | ([], _) => raise THM ("comp_hhf: no unifiers", 1, [tha, thb])
   258   | _  => raise THM ("comp_hhf: multiple unifiers", 1, [tha, thb]));
   259 
   260 
   261 (* non-atomic goal assumptions *)
   262 
   263 fun non_atomic (Const ("==>", _) $ _ $ _) = true
   264   | non_atomic (Const ("all", _) $ _) = true
   265   | non_atomic _ = false;
   266 
   267 fun assume_rule_tac ctxt = norm_hhf_tac THEN' CSUBGOAL (fn (goal, i) =>
   268   let
   269     val ((_, goal'), ctxt') = Variable.focus goal ctxt;
   270     val goal'' = Drule.cterm_rule (singleton (Variable.export ctxt' ctxt)) goal';
   271     val Rs = filter (non_atomic o Thm.term_of) (Drule.strip_imp_prems goal'');
   272     val tacs = Rs |> map (fn R =>
   273       Tactic.etac (MetaSimplifier.norm_hhf (Thm.trivial R)) THEN_ALL_NEW assume_tac);
   274   in fold_rev (curry op APPEND') tacs (K no_tac) i end);
   275 
   276 end;
   277 
   278 structure BasicGoal: BASIC_GOAL = Goal;
   279 open BasicGoal;