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