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