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