src/Pure/goal.ML
author wenzelm
Sun Jan 11 18:18:35 2009 +0100 (2009-01-11)
changeset 29448 34b9652b2f45
parent 29435 a5f84ac14609
child 30473 e0b66c11e7e4
permissions -rw-r--r--
added Goal.future_enabled abstraction -- now also checks that this is already
a future task, which excludes interactive commands of the old toplevel;
     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_enabled: unit -> bool
    24   val future_result: Proof.context -> thm future -> term -> thm
    25   val prove_internal: cterm list -> cterm -> (thm list -> tactic) -> thm
    26   val prove_multi: Proof.context -> string list -> term list -> term list ->
    27     ({prems: thm list, context: Proof.context} -> tactic) -> thm list
    28   val prove_future: Proof.context -> string list -> term list -> term ->
    29     ({prems: thm list, context: Proof.context} -> tactic) -> thm
    30   val prove: Proof.context -> string list -> term list -> term ->
    31     ({prems: thm list, context: Proof.context} -> tactic) -> thm
    32   val prove_global: theory -> string list -> term list -> term ->
    33     ({prems: thm list, context: Proof.context} -> tactic) -> thm
    34   val extract: int -> int -> thm -> thm Seq.seq
    35   val retrofit: int -> int -> thm -> thm -> thm Seq.seq
    36   val conjunction_tac: int -> tactic
    37   val precise_conjunction_tac: int -> int -> tactic
    38   val recover_conjunction_tac: tactic
    39   val norm_hhf_tac: int -> tactic
    40   val compose_hhf_tac: thm -> int -> tactic
    41   val assume_rule_tac: Proof.context -> int -> tactic
    42 end;
    43 
    44 structure Goal: GOAL =
    45 struct
    46 
    47 (** goals **)
    48 
    49 (*
    50   -------- (init)
    51   C ==> #C
    52 *)
    53 val init =
    54   let val A = #1 (Thm.dest_implies (Thm.cprop_of Drule.protectI))
    55   in fn C => Thm.instantiate ([], [(A, C)]) Drule.protectI end;
    56 
    57 (*
    58    C
    59   --- (protect)
    60   #C
    61 *)
    62 fun protect th = Drule.comp_no_flatten (th, 0) 1 Drule.protectI;
    63 
    64 (*
    65   A ==> ... ==> #C
    66   ---------------- (conclude)
    67   A ==> ... ==> C
    68 *)
    69 fun conclude th = Drule.comp_no_flatten (th, Thm.nprems_of th) 1 Drule.protectD;
    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 
    96 (* future_enabled *)
    97 
    98 val parallel_proofs = ref true;
    99 
   100 fun future_enabled () =
   101   Future.enabled () andalso ! parallel_proofs andalso is_some (Future.thread_data ());
   102 
   103 
   104 (* future_result *)
   105 
   106 fun future_result ctxt result prop =
   107   let
   108     val thy = ProofContext.theory_of ctxt;
   109     val _ = Context.reject_draft thy;
   110     val cert = Thm.cterm_of thy;
   111     val certT = Thm.ctyp_of thy;
   112 
   113     val assms = Assumption.assms_of ctxt;
   114     val As = map Thm.term_of assms;
   115 
   116     val xs = map Free (fold Term.add_frees (prop :: As) []);
   117     val fixes = map cert xs;
   118 
   119     val tfrees = fold Term.add_tfrees (prop :: As) [];
   120     val instT = map (fn (a, S) => (certT (TVar ((a, 0), S)), certT (TFree (a, S)))) tfrees;
   121 
   122     val global_prop =
   123       Term.map_types Logic.varifyT (fold_rev Logic.all xs (Logic.list_implies (As, prop)));
   124     val global_result = result |> Future.map
   125       (Thm.adjust_maxidx_thm ~1 #>
   126         Drule.implies_intr_list assms #>
   127         Drule.forall_intr_list fixes #>
   128         Thm.generalize (map #1 tfrees, []) 0);
   129     val local_result =
   130       Thm.future global_result (cert global_prop)
   131       |> Thm.instantiate (instT, [])
   132       |> Drule.forall_elim_list fixes
   133       |> fold (Thm.elim_implies o Thm.assume) assms;
   134   in local_result end;
   135 
   136 
   137 
   138 (** tactical theorem proving **)
   139 
   140 (* prove_internal -- minimal checks, no normalization of result! *)
   141 
   142 fun prove_internal casms cprop tac =
   143   (case SINGLE (tac (map Assumption.assume casms)) (init cprop) of
   144     SOME th => Drule.implies_intr_list casms (finish th)
   145   | NONE => error "Tactic failed.");
   146 
   147 
   148 (* prove_common etc. *)
   149 
   150 fun prove_common immediate ctxt xs asms props tac =
   151   let
   152     val thy = ProofContext.theory_of ctxt;
   153     val string_of_term = Syntax.string_of_term ctxt;
   154 
   155     val pos = Position.thread_data ();
   156     fun err msg = cat_error msg
   157       ("The error(s) above occurred for the goal statement:\n" ^
   158         string_of_term (Logic.list_implies (asms, Logic.mk_conjunction_list props)) ^
   159         (case Position.str_of pos of "" => "" | s => "\n" ^ s));
   160 
   161     fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm (Term.no_dummy_patterns t))
   162       handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
   163     val casms = map cert_safe asms;
   164     val cprops = map cert_safe props;
   165 
   166     val (prems, ctxt') = ctxt
   167       |> Variable.add_fixes_direct xs
   168       |> fold Variable.declare_term (asms @ props)
   169       |> Assumption.add_assumes casms
   170       ||> Variable.set_body true;
   171     val sorts = Variable.sorts_of ctxt';
   172 
   173     val stmt = Thm.weaken_sorts sorts (Conjunction.mk_conjunction_balanced cprops);
   174 
   175     fun result () =
   176       (case SINGLE (tac {prems = prems, context = ctxt'}) (init stmt) of
   177         NONE => err "Tactic failed."
   178       | SOME st =>
   179           let val res = finish st handle THM (msg, _, _) => err msg in
   180             if Unify.matches_list thy [Thm.term_of stmt] [Thm.prop_of res]
   181             then Thm.check_shyps sorts res
   182             else err ("Proved a different theorem: " ^ string_of_term (Thm.prop_of res))
   183           end);
   184     val res =
   185       if immediate orelse #maxidx (Thm.rep_cterm stmt) >= 0 orelse not (future_enabled ())
   186       then result ()
   187       else future_result ctxt' (Future.fork_pri ~1 result) (Thm.term_of stmt);
   188   in
   189     Conjunction.elim_balanced (length props) res
   190     |> map (Assumption.export false ctxt' ctxt)
   191     |> Variable.export ctxt' ctxt
   192     |> map Drule.zero_var_indexes
   193   end;
   194 
   195 val prove_multi = prove_common true;
   196 
   197 fun prove_future ctxt xs asms prop tac = hd (prove_common false ctxt xs asms [prop] tac);
   198 fun prove ctxt xs asms prop tac = hd (prove_common true ctxt xs asms [prop] tac);
   199 
   200 fun prove_global thy xs asms prop tac =
   201   Drule.standard (prove (ProofContext.init thy) xs asms prop tac);
   202 
   203 
   204 
   205 (** goal structure **)
   206 
   207 (* nested goals *)
   208 
   209 fun extract i n st =
   210   (if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st then Seq.empty
   211    else if n = 1 then Seq.single (Thm.cprem_of st i)
   212    else
   213      Seq.single (Conjunction.mk_conjunction_balanced (map (Thm.cprem_of st) (i upto i + n - 1))))
   214   |> Seq.map (Thm.adjust_maxidx_cterm ~1 #> init);
   215 
   216 fun retrofit i n st' st =
   217   (if n = 1 then st
   218    else st |> Drule.with_subgoal i (Conjunction.uncurry_balanced n))
   219   |> Thm.compose_no_flatten false (conclude st', Thm.nprems_of st') i;
   220 
   221 fun SELECT_GOAL tac i st =
   222   if Thm.nprems_of st = 1 andalso i = 1 then tac st
   223   else Seq.lifts (retrofit i 1) (Seq.maps tac (extract i 1 st)) st;
   224 
   225 
   226 (* multiple goals *)
   227 
   228 fun precise_conjunction_tac 0 i = eq_assume_tac i
   229   | precise_conjunction_tac 1 i = SUBGOAL (K all_tac) i
   230   | precise_conjunction_tac n i = PRIMITIVE (Drule.with_subgoal i (Conjunction.curry_balanced n));
   231 
   232 val adhoc_conjunction_tac = REPEAT_ALL_NEW
   233   (SUBGOAL (fn (goal, i) =>
   234     if can Logic.dest_conjunction goal then rtac Conjunction.conjunctionI i
   235     else no_tac));
   236 
   237 val conjunction_tac = SUBGOAL (fn (goal, i) =>
   238   precise_conjunction_tac (length (Logic.dest_conjunctions goal)) i ORELSE
   239   TRY (adhoc_conjunction_tac i));
   240 
   241 val recover_conjunction_tac = PRIMITIVE (fn th =>
   242   Conjunction.uncurry_balanced (Thm.nprems_of th) th);
   243 
   244 fun PRECISE_CONJUNCTS n tac =
   245   SELECT_GOAL (precise_conjunction_tac n 1
   246     THEN tac
   247     THEN recover_conjunction_tac);
   248 
   249 fun CONJUNCTS tac =
   250   SELECT_GOAL (conjunction_tac 1
   251     THEN tac
   252     THEN recover_conjunction_tac);
   253 
   254 
   255 (* hhf normal form *)
   256 
   257 val norm_hhf_tac =
   258   rtac Drule.asm_rl  (*cheap approximation -- thanks to builtin Logic.flatten_params*)
   259   THEN' SUBGOAL (fn (t, i) =>
   260     if Drule.is_norm_hhf t then all_tac
   261     else MetaSimplifier.rewrite_goal_tac Drule.norm_hhf_eqs i);
   262 
   263 fun compose_hhf_tac th i st =
   264   PRIMSEQ (Thm.bicompose false (false, Drule.lift_all (Thm.cprem_of st i) th, 0) i) st;
   265 
   266 
   267 (* non-atomic goal assumptions *)
   268 
   269 fun non_atomic (Const ("==>", _) $ _ $ _) = true
   270   | non_atomic (Const ("all", _) $ _) = true
   271   | non_atomic _ = false;
   272 
   273 fun assume_rule_tac ctxt = norm_hhf_tac THEN' CSUBGOAL (fn (goal, i) =>
   274   let
   275     val ((_, goal'), ctxt') = Variable.focus goal ctxt;
   276     val goal'' = Drule.cterm_rule (singleton (Variable.export ctxt' ctxt)) goal';
   277     val Rs = filter (non_atomic o Thm.term_of) (Drule.strip_imp_prems goal'');
   278     val tacs = Rs |> map (fn R =>
   279       Tactic.etac (MetaSimplifier.norm_hhf (Thm.trivial R)) THEN_ALL_NEW assume_tac);
   280   in fold_rev (curry op APPEND') tacs (K no_tac) i end);
   281 
   282 end;
   283 
   284 structure BasicGoal: BASIC_GOAL = Goal;
   285 open BasicGoal;