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