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