src/Pure/goal.ML
author wenzelm
Sun Jul 19 14:15:47 2009 +0200 (2009-07-19)
changeset 32056 f4b74cbecdaf
parent 30473 e0b66c11e7e4
child 32058 c76fd93b3b99
permissions -rw-r--r--
future_result: explicitly impose Variable.sorts_of again;
     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.all_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       cert (Term.map_types Logic.varifyT (fold_rev Logic.all xs (Logic.list_implies (As, prop))))
   124       |> Thm.weaken_sorts (Variable.sorts_of ctxt);
   125     val global_result = result |> Future.map
   126       (Thm.adjust_maxidx_thm ~1 #>
   127         Drule.implies_intr_list assms #>
   128         Drule.forall_intr_list fixes #>
   129         Thm.generalize (map #1 tfrees, []) 0);
   130     val local_result =
   131       Thm.future global_result global_prop
   132       |> Thm.instantiate (instT, [])
   133       |> Drule.forall_elim_list fixes
   134       |> fold (Thm.elim_implies o Thm.assume) assms;
   135   in local_result end;
   136 
   137 
   138 
   139 (** tactical theorem proving **)
   140 
   141 (* prove_internal -- minimal checks, no normalization of result! *)
   142 
   143 fun prove_internal casms cprop tac =
   144   (case SINGLE (tac (map Assumption.assume casms)) (init cprop) of
   145     SOME th => Drule.implies_intr_list casms (finish th)
   146   | NONE => error "Tactic failed.");
   147 
   148 
   149 (* prove_common etc. *)
   150 
   151 fun prove_common immediate ctxt xs asms props tac =
   152   let
   153     val thy = ProofContext.theory_of ctxt;
   154     val string_of_term = Syntax.string_of_term ctxt;
   155 
   156     val pos = Position.thread_data ();
   157     fun err msg = cat_error msg
   158       ("The error(s) above occurred for the goal statement:\n" ^
   159         string_of_term (Logic.list_implies (asms, Logic.mk_conjunction_list props)) ^
   160         (case Position.str_of pos of "" => "" | s => "\n" ^ s));
   161 
   162     fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm (Term.no_dummy_patterns t))
   163       handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
   164     val casms = map cert_safe asms;
   165     val cprops = map cert_safe props;
   166 
   167     val (prems, ctxt') = ctxt
   168       |> Variable.add_fixes_direct xs
   169       |> fold Variable.declare_term (asms @ props)
   170       |> Assumption.add_assumes casms
   171       ||> Variable.set_body true;
   172     val sorts = Variable.sorts_of ctxt';
   173 
   174     val stmt = Thm.weaken_sorts sorts (Conjunction.mk_conjunction_balanced cprops);
   175 
   176     fun result () =
   177       (case SINGLE (tac {prems = prems, context = ctxt'}) (init stmt) of
   178         NONE => err "Tactic failed."
   179       | SOME st =>
   180           let val res = finish st handle THM (msg, _, _) => err msg in
   181             if Unify.matches_list thy [Thm.term_of stmt] [Thm.prop_of res]
   182             then Thm.check_shyps sorts res
   183             else err ("Proved a different theorem: " ^ string_of_term (Thm.prop_of res))
   184           end);
   185     val res =
   186       if immediate orelse #maxidx (Thm.rep_cterm stmt) >= 0 orelse not (future_enabled ())
   187       then result ()
   188       else future_result ctxt' (Future.fork_pri ~1 result) (Thm.term_of stmt);
   189   in
   190     Conjunction.elim_balanced (length props) res
   191     |> map (Assumption.export false ctxt' ctxt)
   192     |> Variable.export ctxt' ctxt
   193     |> map Drule.zero_var_indexes
   194   end;
   195 
   196 val prove_multi = prove_common true;
   197 
   198 fun prove_future ctxt xs asms prop tac = hd (prove_common false ctxt xs asms [prop] tac);
   199 fun prove ctxt xs asms prop tac = hd (prove_common true ctxt xs asms [prop] tac);
   200 
   201 fun prove_global thy xs asms prop tac =
   202   Drule.standard (prove (ProofContext.init thy) xs asms prop tac);
   203 
   204 
   205 
   206 (** goal structure **)
   207 
   208 (* nested goals *)
   209 
   210 fun extract i n st =
   211   (if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st then Seq.empty
   212    else if n = 1 then Seq.single (Thm.cprem_of st i)
   213    else
   214      Seq.single (Conjunction.mk_conjunction_balanced (map (Thm.cprem_of st) (i upto i + n - 1))))
   215   |> Seq.map (Thm.adjust_maxidx_cterm ~1 #> init);
   216 
   217 fun retrofit i n st' st =
   218   (if n = 1 then st
   219    else st |> Drule.with_subgoal i (Conjunction.uncurry_balanced n))
   220   |> Thm.compose_no_flatten false (conclude st', Thm.nprems_of st') i;
   221 
   222 fun SELECT_GOAL tac i st =
   223   if Thm.nprems_of st = 1 andalso i = 1 then tac st
   224   else Seq.lifts (retrofit i 1) (Seq.maps tac (extract i 1 st)) st;
   225 
   226 
   227 (* multiple goals *)
   228 
   229 fun precise_conjunction_tac 0 i = eq_assume_tac i
   230   | precise_conjunction_tac 1 i = SUBGOAL (K all_tac) i
   231   | precise_conjunction_tac n i = PRIMITIVE (Drule.with_subgoal i (Conjunction.curry_balanced n));
   232 
   233 val adhoc_conjunction_tac = REPEAT_ALL_NEW
   234   (SUBGOAL (fn (goal, i) =>
   235     if can Logic.dest_conjunction goal then rtac Conjunction.conjunctionI i
   236     else no_tac));
   237 
   238 val conjunction_tac = SUBGOAL (fn (goal, i) =>
   239   precise_conjunction_tac (length (Logic.dest_conjunctions goal)) i ORELSE
   240   TRY (adhoc_conjunction_tac i));
   241 
   242 val recover_conjunction_tac = PRIMITIVE (fn th =>
   243   Conjunction.uncurry_balanced (Thm.nprems_of th) th);
   244 
   245 fun PRECISE_CONJUNCTS n tac =
   246   SELECT_GOAL (precise_conjunction_tac n 1
   247     THEN tac
   248     THEN recover_conjunction_tac);
   249 
   250 fun CONJUNCTS tac =
   251   SELECT_GOAL (conjunction_tac 1
   252     THEN tac
   253     THEN recover_conjunction_tac);
   254 
   255 
   256 (* hhf normal form *)
   257 
   258 val norm_hhf_tac =
   259   rtac Drule.asm_rl  (*cheap approximation -- thanks to builtin Logic.flatten_params*)
   260   THEN' SUBGOAL (fn (t, i) =>
   261     if Drule.is_norm_hhf t then all_tac
   262     else MetaSimplifier.rewrite_goal_tac Drule.norm_hhf_eqs i);
   263 
   264 fun compose_hhf_tac th i st =
   265   PRIMSEQ (Thm.bicompose false (false, Drule.lift_all (Thm.cprem_of st i) th, 0) i) st;
   266 
   267 
   268 (* non-atomic goal assumptions *)
   269 
   270 fun non_atomic (Const ("==>", _) $ _ $ _) = true
   271   | non_atomic (Const ("all", _) $ _) = true
   272   | non_atomic _ = false;
   273 
   274 fun assume_rule_tac ctxt = norm_hhf_tac THEN' CSUBGOAL (fn (goal, i) =>
   275   let
   276     val ((_, goal'), ctxt') = Variable.focus goal ctxt;
   277     val goal'' = Drule.cterm_rule (singleton (Variable.export ctxt' ctxt)) goal';
   278     val Rs = filter (non_atomic o Thm.term_of) (Drule.strip_imp_prems goal'');
   279     val tacs = Rs |> map (fn R =>
   280       Tactic.etac (MetaSimplifier.norm_hhf (Thm.trivial R)) THEN_ALL_NEW assume_tac);
   281   in fold_rev (curry op APPEND') tacs (K no_tac) i end);
   282 
   283 end;
   284 
   285 structure BasicGoal: BASIC_GOAL = Goal;
   286 open BasicGoal;