src/Pure/goal.ML
author wenzelm
Sat Apr 12 17:00:40 2008 +0200 (2008-04-12)
changeset 26628 63306cb94313
parent 25301 24e027f55f45
child 26713 1c6181def1d0
permissions -rw-r--r--
replaced Drule.close_derivation/Goal.close_result by Thm.close_derivation (removed obsolete compression);
wenzelm@17980
     1
(*  Title:      Pure/goal.ML
wenzelm@17980
     2
    ID:         $Id$
wenzelm@17980
     3
    Author:     Makarius and Lawrence C Paulson
wenzelm@17980
     4
wenzelm@18139
     5
Goals in tactical theorem proving.
wenzelm@17980
     6
*)
wenzelm@17980
     7
wenzelm@17980
     8
signature BASIC_GOAL =
wenzelm@17980
     9
sig
wenzelm@17980
    10
  val SELECT_GOAL: tactic -> int -> tactic
wenzelm@23418
    11
  val CONJUNCTS: tactic -> int -> tactic
wenzelm@23414
    12
  val PRECISE_CONJUNCTS: int -> tactic -> int -> tactic
wenzelm@17980
    13
end;
wenzelm@17980
    14
wenzelm@17980
    15
signature GOAL =
wenzelm@17980
    16
sig
wenzelm@17980
    17
  include BASIC_GOAL
wenzelm@17980
    18
  val init: cterm -> thm
wenzelm@18027
    19
  val protect: thm -> thm
wenzelm@17980
    20
  val conclude: thm -> thm
wenzelm@17980
    21
  val finish: thm -> thm
wenzelm@21604
    22
  val norm_result: thm -> thm
wenzelm@23356
    23
  val prove_internal: cterm list -> cterm -> (thm list -> tactic) -> thm
wenzelm@20290
    24
  val prove_multi: Proof.context -> string list -> term list -> term list ->
wenzelm@20290
    25
    ({prems: thm list, context: Proof.context} -> tactic) -> thm list
wenzelm@20290
    26
  val prove: Proof.context -> string list -> term list -> term ->
wenzelm@20290
    27
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@20056
    28
  val prove_global: theory -> string list -> term list -> term -> (thm list -> tactic) -> thm
wenzelm@19184
    29
  val extract: int -> int -> thm -> thm Seq.seq
wenzelm@19184
    30
  val retrofit: int -> int -> thm -> thm -> thm Seq.seq
wenzelm@23418
    31
  val conjunction_tac: int -> tactic
wenzelm@23414
    32
  val precise_conjunction_tac: int -> int -> tactic
wenzelm@23418
    33
  val recover_conjunction_tac: tactic
wenzelm@21687
    34
  val norm_hhf_tac: int -> tactic
wenzelm@21687
    35
  val compose_hhf_tac: thm -> int -> tactic
wenzelm@23237
    36
  val assume_rule_tac: Proof.context -> int -> tactic
wenzelm@17980
    37
end;
wenzelm@17980
    38
wenzelm@17980
    39
structure Goal: GOAL =
wenzelm@17980
    40
struct
wenzelm@17980
    41
wenzelm@18027
    42
(** goals **)
wenzelm@18027
    43
wenzelm@18027
    44
(*
wenzelm@18027
    45
  -------- (init)
wenzelm@18027
    46
  C ==> #C
wenzelm@18027
    47
*)
wenzelm@20290
    48
val init =
wenzelm@22902
    49
  let val A = #1 (Thm.dest_implies (Thm.cprop_of Drule.protectI))
wenzelm@20290
    50
  in fn C => Thm.instantiate ([], [(A, C)]) Drule.protectI end;
wenzelm@17980
    51
wenzelm@17980
    52
(*
wenzelm@18119
    53
   C
wenzelm@18119
    54
  --- (protect)
wenzelm@18119
    55
  #C
wenzelm@17980
    56
*)
wenzelm@21579
    57
fun protect th = th COMP_INCR Drule.protectI;
wenzelm@17980
    58
wenzelm@17980
    59
(*
wenzelm@18027
    60
  A ==> ... ==> #C
wenzelm@18027
    61
  ---------------- (conclude)
wenzelm@17980
    62
  A ==> ... ==> C
wenzelm@17980
    63
*)
wenzelm@17980
    64
fun conclude th =
wenzelm@18497
    65
  (case SINGLE (Thm.compose_no_flatten false (th, Thm.nprems_of th) 1)
wenzelm@18027
    66
      (Drule.incr_indexes th Drule.protectD) of
wenzelm@17980
    67
    SOME th' => th'
wenzelm@17980
    68
  | NONE => raise THM ("Failed to conclude goal", 0, [th]));
wenzelm@17980
    69
wenzelm@17980
    70
(*
wenzelm@18027
    71
  #C
wenzelm@18027
    72
  --- (finish)
wenzelm@18027
    73
   C
wenzelm@17983
    74
*)
wenzelm@17980
    75
fun finish th =
wenzelm@17980
    76
  (case Thm.nprems_of th of
wenzelm@17980
    77
    0 => conclude th
wenzelm@17980
    78
  | n => raise THM ("Proof failed.\n" ^
wenzelm@17980
    79
      Pretty.string_of (Pretty.chunks (Display.pretty_goals n th)) ^
wenzelm@17980
    80
      ("\n" ^ string_of_int n ^ " unsolved goal(s)!"), 0, [th]));
wenzelm@17980
    81
wenzelm@17980
    82
wenzelm@18027
    83
wenzelm@18027
    84
(** results **)
wenzelm@18027
    85
wenzelm@21604
    86
val norm_result =
wenzelm@21604
    87
  Drule.flexflex_unique
wenzelm@21604
    88
  #> MetaSimplifier.norm_hhf_protect
wenzelm@21604
    89
  #> Thm.strip_shyps
wenzelm@21604
    90
  #> Drule.zero_var_indexes;
wenzelm@21604
    91
wenzelm@21604
    92
wenzelm@18027
    93
wenzelm@18027
    94
(** tactical theorem proving **)
wenzelm@18027
    95
wenzelm@23356
    96
(* prove_internal -- minimal checks, no normalization of result! *)
wenzelm@20250
    97
wenzelm@23356
    98
fun prove_internal casms cprop tac =
wenzelm@20250
    99
  (case SINGLE (tac (map Assumption.assume casms)) (init cprop) of
wenzelm@20250
   100
    SOME th => Drule.implies_intr_list casms (finish th)
wenzelm@20250
   101
  | NONE => error "Tactic failed.");
wenzelm@20250
   102
wenzelm@20250
   103
wenzelm@18119
   104
(* prove_multi *)
wenzelm@17986
   105
wenzelm@20056
   106
fun prove_multi ctxt xs asms props tac =
wenzelm@17980
   107
  let
wenzelm@21516
   108
    val thy = ProofContext.theory_of ctxt;
wenzelm@20056
   109
    val string_of_term = Sign.string_of_term thy;
wenzelm@20056
   110
wenzelm@20250
   111
    fun err msg = cat_error msg
wenzelm@20250
   112
      ("The error(s) above occurred for the goal statement:\n" ^
wenzelm@20250
   113
        string_of_term (Logic.list_implies (asms, Logic.mk_conjunction_list props)));
wenzelm@17980
   114
wenzelm@20250
   115
    fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm (Term.no_dummy_patterns t))
wenzelm@17980
   116
      handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
wenzelm@20250
   117
    val casms = map cert_safe asms;
wenzelm@20250
   118
    val cprops = map cert_safe props;
wenzelm@17980
   119
wenzelm@20250
   120
    val (prems, ctxt') = ctxt
wenzelm@20250
   121
      |> Variable.add_fixes_direct xs
wenzelm@20250
   122
      |> fold Variable.declare_internal (asms @ props)
wenzelm@20250
   123
      |> Assumption.add_assumes casms;
wenzelm@17980
   124
wenzelm@23418
   125
    val goal = init (Conjunction.mk_conjunction_balanced cprops);
wenzelm@19774
   126
    val res =
wenzelm@20250
   127
      (case SINGLE (tac {prems = prems, context = ctxt'}) goal of
wenzelm@19774
   128
        NONE => err "Tactic failed."
wenzelm@19774
   129
      | SOME res => res);
wenzelm@23418
   130
    val results = Conjunction.elim_balanced (length props) (finish res)
wenzelm@20250
   131
      handle THM (msg, _, _) => err msg;
wenzelm@20250
   132
    val _ = Unify.matches_list thy (map Thm.term_of cprops) (map Thm.prop_of results)
wenzelm@20056
   133
      orelse err ("Proved a different theorem: " ^ string_of_term (Thm.prop_of res));
wenzelm@17980
   134
  in
wenzelm@20056
   135
    results
wenzelm@20290
   136
    |> map (Assumption.export false ctxt' ctxt)
wenzelm@20056
   137
    |> Variable.export ctxt' ctxt
wenzelm@20250
   138
    |> map Drule.zero_var_indexes
wenzelm@17980
   139
  end;
wenzelm@17980
   140
wenzelm@17980
   141
wenzelm@18119
   142
(* prove *)
wenzelm@17980
   143
wenzelm@20056
   144
fun prove ctxt xs asms prop tac = hd (prove_multi ctxt xs asms [prop] tac);
wenzelm@20056
   145
wenzelm@20056
   146
fun prove_global thy xs asms prop tac =
wenzelm@21516
   147
  Drule.standard (prove (ProofContext.init thy) xs asms prop (fn {prems, ...} => tac prems));
wenzelm@18027
   148
wenzelm@18027
   149
wenzelm@17980
   150
wenzelm@21687
   151
(** goal structure **)
wenzelm@21687
   152
wenzelm@21687
   153
(* nested goals *)
wenzelm@18207
   154
wenzelm@19184
   155
fun extract i n st =
wenzelm@19184
   156
  (if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st then Seq.empty
wenzelm@19184
   157
   else if n = 1 then Seq.single (Thm.cprem_of st i)
wenzelm@23418
   158
   else
wenzelm@23418
   159
     Seq.single (Conjunction.mk_conjunction_balanced (map (Thm.cprem_of st) (i upto i + n - 1))))
wenzelm@20260
   160
  |> Seq.map (Thm.adjust_maxidx_cterm ~1 #> init);
wenzelm@17980
   161
wenzelm@19221
   162
fun retrofit i n st' st =
wenzelm@19221
   163
  (if n = 1 then st
wenzelm@23418
   164
   else st |> Drule.with_subgoal i (Conjunction.uncurry_balanced n))
wenzelm@19221
   165
  |> Thm.compose_no_flatten false (conclude st', Thm.nprems_of st') i;
wenzelm@18207
   166
wenzelm@17980
   167
fun SELECT_GOAL tac i st =
wenzelm@19191
   168
  if Thm.nprems_of st = 1 andalso i = 1 then tac st
wenzelm@19184
   169
  else Seq.lifts (retrofit i 1) (Seq.maps tac (extract i 1 st)) st;
wenzelm@17980
   170
wenzelm@21687
   171
wenzelm@21687
   172
(* multiple goals *)
wenzelm@21687
   173
wenzelm@23418
   174
fun precise_conjunction_tac 0 i = eq_assume_tac i
wenzelm@23418
   175
  | precise_conjunction_tac 1 i = SUBGOAL (K all_tac) i
wenzelm@23418
   176
  | precise_conjunction_tac n i = PRIMITIVE (Drule.with_subgoal i (Conjunction.curry_balanced n));
wenzelm@23414
   177
wenzelm@23418
   178
val adhoc_conjunction_tac = REPEAT_ALL_NEW
wenzelm@23418
   179
  (SUBGOAL (fn (goal, i) =>
wenzelm@23418
   180
    if can Logic.dest_conjunction goal then rtac Conjunction.conjunctionI i
wenzelm@23418
   181
    else no_tac));
wenzelm@21687
   182
wenzelm@23418
   183
val conjunction_tac = SUBGOAL (fn (goal, i) =>
wenzelm@23418
   184
  precise_conjunction_tac (length (Logic.dest_conjunctions goal)) i ORELSE
wenzelm@23418
   185
  TRY (adhoc_conjunction_tac i));
wenzelm@21687
   186
wenzelm@23418
   187
val recover_conjunction_tac = PRIMITIVE (fn th =>
wenzelm@23418
   188
  Conjunction.uncurry_balanced (Thm.nprems_of th) th);
wenzelm@23414
   189
wenzelm@23414
   190
fun PRECISE_CONJUNCTS n tac =
wenzelm@23414
   191
  SELECT_GOAL (precise_conjunction_tac n 1
wenzelm@23414
   192
    THEN tac
wenzelm@23418
   193
    THEN recover_conjunction_tac);
wenzelm@23414
   194
wenzelm@21687
   195
fun CONJUNCTS tac =
wenzelm@21687
   196
  SELECT_GOAL (conjunction_tac 1
wenzelm@21687
   197
    THEN tac
wenzelm@23418
   198
    THEN recover_conjunction_tac);
wenzelm@21687
   199
wenzelm@21687
   200
wenzelm@21687
   201
(* hhf normal form *)
wenzelm@21687
   202
wenzelm@21687
   203
val norm_hhf_tac =
wenzelm@21687
   204
  rtac Drule.asm_rl  (*cheap approximation -- thanks to builtin Logic.flatten_params*)
wenzelm@21687
   205
  THEN' SUBGOAL (fn (t, i) =>
wenzelm@21687
   206
    if Drule.is_norm_hhf t then all_tac
wenzelm@23536
   207
    else MetaSimplifier.rewrite_goal_tac [Drule.norm_hhf_eq] i);
wenzelm@21687
   208
wenzelm@25301
   209
fun compose_hhf_tac th i st =
wenzelm@25301
   210
  PRIMSEQ (Thm.bicompose false (false, Drule.lift_all (Thm.cprem_of st i) th, 0) i) st;
wenzelm@21687
   211
wenzelm@23237
   212
wenzelm@23237
   213
(* non-atomic goal assumptions *)
wenzelm@23237
   214
wenzelm@23356
   215
fun non_atomic (Const ("==>", _) $ _ $ _) = true
wenzelm@23356
   216
  | non_atomic (Const ("all", _) $ _) = true
wenzelm@23356
   217
  | non_atomic _ = false;
wenzelm@23356
   218
wenzelm@23237
   219
fun assume_rule_tac ctxt = norm_hhf_tac THEN' CSUBGOAL (fn (goal, i) =>
wenzelm@23237
   220
  let
wenzelm@23237
   221
    val ((_, goal'), ctxt') = Variable.focus goal ctxt;
wenzelm@23237
   222
    val goal'' = Drule.cterm_rule (singleton (Variable.export ctxt' ctxt)) goal';
wenzelm@23356
   223
    val Rs = filter (non_atomic o Thm.term_of) (Drule.strip_imp_prems goal'');
wenzelm@23237
   224
    val tacs = Rs |> map (fn R =>
wenzelm@23237
   225
      Tactic.etac (MetaSimplifier.norm_hhf (Thm.trivial R)) THEN_ALL_NEW assume_tac);
wenzelm@23237
   226
  in fold_rev (curry op APPEND') tacs (K no_tac) i end);
wenzelm@23237
   227
wenzelm@18207
   228
end;
wenzelm@18207
   229
wenzelm@17980
   230
structure BasicGoal: BASIC_GOAL = Goal;
wenzelm@17980
   231
open BasicGoal;