src/Pure/goal.ML
author wenzelm
Thu Apr 17 22:22:25 2008 +0200 (2008-04-17)
changeset 26713 1c6181def1d0
parent 26628 63306cb94313
child 26939 1035c89b4c02
permissions -rw-r--r--
prove_global: Variable.set_body true, pass context;
     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 ->
    29     ({prems: thm list, context: Proof.context} -> 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 val norm_result =
    88   Drule.flexflex_unique
    89   #> MetaSimplifier.norm_hhf_protect
    90   #> Thm.strip_shyps
    91   #> Drule.zero_var_indexes;
    92 
    93 
    94 
    95 (** tactical theorem proving **)
    96 
    97 (* prove_internal -- minimal checks, no normalization of result! *)
    98 
    99 fun prove_internal casms cprop tac =
   100   (case SINGLE (tac (map Assumption.assume casms)) (init cprop) of
   101     SOME th => Drule.implies_intr_list casms (finish th)
   102   | NONE => error "Tactic failed.");
   103 
   104 
   105 (* prove_multi *)
   106 
   107 fun prove_multi ctxt xs asms props tac =
   108   let
   109     val thy = ProofContext.theory_of ctxt;
   110     val string_of_term = Sign.string_of_term thy;
   111 
   112     fun err msg = cat_error msg
   113       ("The error(s) above occurred for the goal statement:\n" ^
   114         string_of_term (Logic.list_implies (asms, Logic.mk_conjunction_list props)));
   115 
   116     fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm (Term.no_dummy_patterns t))
   117       handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
   118     val casms = map cert_safe asms;
   119     val cprops = map cert_safe props;
   120 
   121     val (prems, ctxt') = ctxt
   122       |> Variable.add_fixes_direct xs
   123       |> fold Variable.declare_internal (asms @ props)
   124       |> Assumption.add_assumes casms
   125       ||> Variable.set_body true;
   126 
   127     val goal = init (Conjunction.mk_conjunction_balanced cprops);
   128     val res =
   129       (case SINGLE (tac {prems = prems, context = ctxt'}) goal of
   130         NONE => err "Tactic failed."
   131       | SOME res => res);
   132     val results = Conjunction.elim_balanced (length props) (finish res)
   133       handle THM (msg, _, _) => err msg;
   134     val _ = Unify.matches_list thy (map Thm.term_of cprops) (map Thm.prop_of results)
   135       orelse err ("Proved a different theorem: " ^ string_of_term (Thm.prop_of res));
   136   in
   137     results
   138     |> map (Assumption.export false ctxt' ctxt)
   139     |> Variable.export ctxt' ctxt
   140     |> map Drule.zero_var_indexes
   141   end;
   142 
   143 
   144 (* prove *)
   145 
   146 fun prove ctxt xs asms prop tac = hd (prove_multi ctxt xs asms [prop] tac);
   147 
   148 fun prove_global thy xs asms prop tac =
   149   Drule.standard (prove (ProofContext.init thy) xs asms prop tac);
   150 
   151 
   152 
   153 (** goal structure **)
   154 
   155 (* nested goals *)
   156 
   157 fun extract i n st =
   158   (if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st then Seq.empty
   159    else if n = 1 then Seq.single (Thm.cprem_of st i)
   160    else
   161      Seq.single (Conjunction.mk_conjunction_balanced (map (Thm.cprem_of st) (i upto i + n - 1))))
   162   |> Seq.map (Thm.adjust_maxidx_cterm ~1 #> init);
   163 
   164 fun retrofit i n st' st =
   165   (if n = 1 then st
   166    else st |> Drule.with_subgoal i (Conjunction.uncurry_balanced n))
   167   |> Thm.compose_no_flatten false (conclude st', Thm.nprems_of st') i;
   168 
   169 fun SELECT_GOAL tac i st =
   170   if Thm.nprems_of st = 1 andalso i = 1 then tac st
   171   else Seq.lifts (retrofit i 1) (Seq.maps tac (extract i 1 st)) st;
   172 
   173 
   174 (* multiple goals *)
   175 
   176 fun precise_conjunction_tac 0 i = eq_assume_tac i
   177   | precise_conjunction_tac 1 i = SUBGOAL (K all_tac) i
   178   | precise_conjunction_tac n i = PRIMITIVE (Drule.with_subgoal i (Conjunction.curry_balanced n));
   179 
   180 val adhoc_conjunction_tac = REPEAT_ALL_NEW
   181   (SUBGOAL (fn (goal, i) =>
   182     if can Logic.dest_conjunction goal then rtac Conjunction.conjunctionI i
   183     else no_tac));
   184 
   185 val conjunction_tac = SUBGOAL (fn (goal, i) =>
   186   precise_conjunction_tac (length (Logic.dest_conjunctions goal)) i ORELSE
   187   TRY (adhoc_conjunction_tac i));
   188 
   189 val recover_conjunction_tac = PRIMITIVE (fn th =>
   190   Conjunction.uncurry_balanced (Thm.nprems_of th) th);
   191 
   192 fun PRECISE_CONJUNCTS n tac =
   193   SELECT_GOAL (precise_conjunction_tac n 1
   194     THEN tac
   195     THEN recover_conjunction_tac);
   196 
   197 fun CONJUNCTS tac =
   198   SELECT_GOAL (conjunction_tac 1
   199     THEN tac
   200     THEN recover_conjunction_tac);
   201 
   202 
   203 (* hhf normal form *)
   204 
   205 val norm_hhf_tac =
   206   rtac Drule.asm_rl  (*cheap approximation -- thanks to builtin Logic.flatten_params*)
   207   THEN' SUBGOAL (fn (t, i) =>
   208     if Drule.is_norm_hhf t then all_tac
   209     else MetaSimplifier.rewrite_goal_tac [Drule.norm_hhf_eq] i);
   210 
   211 fun compose_hhf_tac th i st =
   212   PRIMSEQ (Thm.bicompose false (false, Drule.lift_all (Thm.cprem_of st i) th, 0) i) st;
   213 
   214 
   215 (* non-atomic goal assumptions *)
   216 
   217 fun non_atomic (Const ("==>", _) $ _ $ _) = true
   218   | non_atomic (Const ("all", _) $ _) = true
   219   | non_atomic _ = false;
   220 
   221 fun assume_rule_tac ctxt = norm_hhf_tac THEN' CSUBGOAL (fn (goal, i) =>
   222   let
   223     val ((_, goal'), ctxt') = Variable.focus goal ctxt;
   224     val goal'' = Drule.cterm_rule (singleton (Variable.export ctxt' ctxt)) goal';
   225     val Rs = filter (non_atomic o Thm.term_of) (Drule.strip_imp_prems goal'');
   226     val tacs = Rs |> map (fn R =>
   227       Tactic.etac (MetaSimplifier.norm_hhf (Thm.trivial R)) THEN_ALL_NEW assume_tac);
   228   in fold_rev (curry op APPEND') tacs (K no_tac) i end);
   229 
   230 end;
   231 
   232 structure BasicGoal: BASIC_GOAL = Goal;
   233 open BasicGoal;