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