src/Pure/goal.ML
author wenzelm
Sat Jan 10 21:32:30 2009 +0100 (2009-01-10)
changeset 29435 a5f84ac14609
parent 29345 5904873d8f11
child 29448 34b9652b2f45
permissions -rw-r--r--
added parallel_proofs flag (default true, cf. usedir option -Q), which can be disabled in low-memory situations;
     1 (*  Title:      Pure/goal.ML
     2     Author:     Makarius
     3 
     4 Goals in tactical theorem proving.
     5 *)
     6 
     7 signature BASIC_GOAL =
     8 sig
     9   val parallel_proofs: bool ref
    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 future_result: Proof.context -> thm future -> term -> 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_future: Proof.context -> string list -> term list -> term ->
    28     ({prems: thm list, context: Proof.context} -> tactic) -> thm
    29   val prove: Proof.context -> string list -> term list -> term ->
    30     ({prems: thm list, context: Proof.context} -> tactic) -> thm
    31   val prove_global: theory -> string list -> term list -> term ->
    32     ({prems: thm list, context: Proof.context} -> tactic) -> thm
    33   val extract: int -> int -> thm -> thm Seq.seq
    34   val retrofit: int -> int -> thm -> thm -> thm Seq.seq
    35   val conjunction_tac: int -> tactic
    36   val precise_conjunction_tac: int -> int -> tactic
    37   val recover_conjunction_tac: tactic
    38   val norm_hhf_tac: int -> tactic
    39   val compose_hhf_tac: thm -> int -> tactic
    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 = Drule.comp_no_flatten (th, 0) 1 Drule.protectI;
    62 
    63 (*
    64   A ==> ... ==> #C
    65   ---------------- (conclude)
    66   A ==> ... ==> C
    67 *)
    68 fun conclude th = Drule.comp_no_flatten (th, Thm.nprems_of th) 1 Drule.protectD;
    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 (* normal form *)
    87 
    88 val norm_result =
    89   Drule.flexflex_unique
    90   #> MetaSimplifier.norm_hhf_protect
    91   #> Thm.strip_shyps
    92   #> Drule.zero_var_indexes;
    93 
    94 
    95 (* future_result *)
    96 
    97 fun future_result ctxt result prop =
    98   let
    99     val thy = ProofContext.theory_of ctxt;
   100     val _ = Context.reject_draft thy;
   101     val cert = Thm.cterm_of thy;
   102     val certT = Thm.ctyp_of thy;
   103 
   104     val assms = Assumption.assms_of ctxt;
   105     val As = map Thm.term_of assms;
   106 
   107     val xs = map Free (fold Term.add_frees (prop :: As) []);
   108     val fixes = map cert xs;
   109 
   110     val tfrees = fold Term.add_tfrees (prop :: As) [];
   111     val instT = map (fn (a, S) => (certT (TVar ((a, 0), S)), certT (TFree (a, S)))) tfrees;
   112 
   113     val global_prop =
   114       Term.map_types Logic.varifyT (fold_rev Logic.all xs (Logic.list_implies (As, prop)));
   115     val global_result = result |> Future.map
   116       (Thm.adjust_maxidx_thm ~1 #>
   117         Drule.implies_intr_list assms #>
   118         Drule.forall_intr_list fixes #>
   119         Thm.generalize (map #1 tfrees, []) 0);
   120     val local_result =
   121       Thm.future global_result (cert global_prop)
   122       |> Thm.instantiate (instT, [])
   123       |> Drule.forall_elim_list fixes
   124       |> fold (Thm.elim_implies o Thm.assume) assms;
   125   in local_result end;
   126 
   127 val parallel_proofs = ref true;
   128 
   129 
   130 
   131 (** tactical theorem proving **)
   132 
   133 (* prove_internal -- minimal checks, no normalization of result! *)
   134 
   135 fun prove_internal casms cprop tac =
   136   (case SINGLE (tac (map Assumption.assume casms)) (init cprop) of
   137     SOME th => Drule.implies_intr_list casms (finish th)
   138   | NONE => error "Tactic failed.");
   139 
   140 
   141 (* prove_common etc. *)
   142 
   143 fun prove_common immediate ctxt xs asms props tac =
   144   let
   145     val thy = ProofContext.theory_of ctxt;
   146     val string_of_term = Syntax.string_of_term ctxt;
   147 
   148     val pos = Position.thread_data ();
   149     fun err msg = cat_error msg
   150       ("The error(s) above occurred for the goal statement:\n" ^
   151         string_of_term (Logic.list_implies (asms, Logic.mk_conjunction_list props)) ^
   152         (case Position.str_of pos of "" => "" | s => "\n" ^ s));
   153 
   154     fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm (Term.no_dummy_patterns t))
   155       handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
   156     val casms = map cert_safe asms;
   157     val cprops = map cert_safe props;
   158 
   159     val (prems, ctxt') = ctxt
   160       |> Variable.add_fixes_direct xs
   161       |> fold Variable.declare_term (asms @ props)
   162       |> Assumption.add_assumes casms
   163       ||> Variable.set_body true;
   164     val sorts = Variable.sorts_of ctxt';
   165 
   166     val stmt = Thm.weaken_sorts sorts (Conjunction.mk_conjunction_balanced cprops);
   167 
   168     fun result () =
   169       (case SINGLE (tac {prems = prems, context = ctxt'}) (init stmt) of
   170         NONE => err "Tactic failed."
   171       | SOME st =>
   172           let val res = finish st handle THM (msg, _, _) => err msg in
   173             if Unify.matches_list thy [Thm.term_of stmt] [Thm.prop_of res]
   174             then Thm.check_shyps sorts res
   175             else err ("Proved a different theorem: " ^ string_of_term (Thm.prop_of res))
   176           end);
   177     val res =
   178       if immediate orelse #maxidx (Thm.rep_cterm stmt) >= 0 orelse
   179         not (Future.enabled () andalso ! parallel_proofs)
   180       then result ()
   181       else future_result ctxt' (Future.fork_pri ~1 result) (Thm.term_of stmt);
   182   in
   183     Conjunction.elim_balanced (length props) res
   184     |> map (Assumption.export false ctxt' ctxt)
   185     |> Variable.export ctxt' ctxt
   186     |> map Drule.zero_var_indexes
   187   end;
   188 
   189 val prove_multi = prove_common true;
   190 
   191 fun prove_future ctxt xs asms prop tac = hd (prove_common false ctxt xs asms [prop] tac);
   192 fun prove ctxt xs asms prop tac = hd (prove_common true ctxt xs asms [prop] tac);
   193 
   194 fun prove_global thy xs asms prop tac =
   195   Drule.standard (prove (ProofContext.init thy) xs asms prop tac);
   196 
   197 
   198 
   199 (** goal structure **)
   200 
   201 (* nested goals *)
   202 
   203 fun extract i n st =
   204   (if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st then Seq.empty
   205    else if n = 1 then Seq.single (Thm.cprem_of st i)
   206    else
   207      Seq.single (Conjunction.mk_conjunction_balanced (map (Thm.cprem_of st) (i upto i + n - 1))))
   208   |> Seq.map (Thm.adjust_maxidx_cterm ~1 #> init);
   209 
   210 fun retrofit i n st' st =
   211   (if n = 1 then st
   212    else st |> Drule.with_subgoal i (Conjunction.uncurry_balanced n))
   213   |> Thm.compose_no_flatten false (conclude st', Thm.nprems_of st') i;
   214 
   215 fun SELECT_GOAL tac i st =
   216   if Thm.nprems_of st = 1 andalso i = 1 then tac st
   217   else Seq.lifts (retrofit i 1) (Seq.maps tac (extract i 1 st)) st;
   218 
   219 
   220 (* multiple goals *)
   221 
   222 fun precise_conjunction_tac 0 i = eq_assume_tac i
   223   | precise_conjunction_tac 1 i = SUBGOAL (K all_tac) i
   224   | precise_conjunction_tac n i = PRIMITIVE (Drule.with_subgoal i (Conjunction.curry_balanced n));
   225 
   226 val adhoc_conjunction_tac = REPEAT_ALL_NEW
   227   (SUBGOAL (fn (goal, i) =>
   228     if can Logic.dest_conjunction goal then rtac Conjunction.conjunctionI i
   229     else no_tac));
   230 
   231 val conjunction_tac = SUBGOAL (fn (goal, i) =>
   232   precise_conjunction_tac (length (Logic.dest_conjunctions goal)) i ORELSE
   233   TRY (adhoc_conjunction_tac i));
   234 
   235 val recover_conjunction_tac = PRIMITIVE (fn th =>
   236   Conjunction.uncurry_balanced (Thm.nprems_of th) th);
   237 
   238 fun PRECISE_CONJUNCTS n tac =
   239   SELECT_GOAL (precise_conjunction_tac n 1
   240     THEN tac
   241     THEN recover_conjunction_tac);
   242 
   243 fun CONJUNCTS tac =
   244   SELECT_GOAL (conjunction_tac 1
   245     THEN tac
   246     THEN recover_conjunction_tac);
   247 
   248 
   249 (* hhf normal form *)
   250 
   251 val norm_hhf_tac =
   252   rtac Drule.asm_rl  (*cheap approximation -- thanks to builtin Logic.flatten_params*)
   253   THEN' SUBGOAL (fn (t, i) =>
   254     if Drule.is_norm_hhf t then all_tac
   255     else MetaSimplifier.rewrite_goal_tac Drule.norm_hhf_eqs i);
   256 
   257 fun compose_hhf_tac th i st =
   258   PRIMSEQ (Thm.bicompose false (false, Drule.lift_all (Thm.cprem_of st i) th, 0) i) st;
   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;