src/Pure/goal.ML
author wenzelm
Tue Sep 23 22:04:30 2008 +0200 (2008-09-23)
changeset 28340 e8597242f649
parent 27218 4548c83cd508
child 28341 383f512314b9
permissions -rw-r--r--
added promise_result, prove_promise;
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@28340
    23
  val promise_result: Proof.context -> (unit -> thm) -> term -> 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@28340
    27
  val prove_promise: Proof.context -> string list -> term list -> term ->
wenzelm@28340
    28
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@20290
    29
  val prove: Proof.context -> string list -> term list -> term ->
wenzelm@20290
    30
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@26713
    31
  val prove_global: theory -> string list -> term list -> term ->
wenzelm@26713
    32
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@19184
    33
  val extract: int -> int -> thm -> thm Seq.seq
wenzelm@19184
    34
  val retrofit: int -> int -> thm -> thm -> thm Seq.seq
wenzelm@23418
    35
  val conjunction_tac: int -> tactic
wenzelm@23414
    36
  val precise_conjunction_tac: int -> int -> tactic
wenzelm@23418
    37
  val recover_conjunction_tac: tactic
wenzelm@21687
    38
  val norm_hhf_tac: int -> tactic
wenzelm@21687
    39
  val compose_hhf_tac: thm -> int -> tactic
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@28340
    90
(* normal form *)
wenzelm@28340
    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
wenzelm@28340
    99
(* promise *)
wenzelm@28340
   100
wenzelm@28340
   101
fun promise_result ctxt result prop =
wenzelm@28340
   102
  let
wenzelm@28340
   103
    val thy = ProofContext.theory_of ctxt;
wenzelm@28340
   104
    val _ = Context.reject_draft thy;
wenzelm@28340
   105
    val cert = Thm.cterm_of thy;
wenzelm@28340
   106
    val certT = Thm.ctyp_of thy;
wenzelm@28340
   107
wenzelm@28340
   108
    val assms = Assumption.assms_of ctxt;
wenzelm@28340
   109
    val As = map Thm.term_of assms;
wenzelm@28340
   110
wenzelm@28340
   111
    val xs = map Free (fold Term.add_frees (prop :: As) []);
wenzelm@28340
   112
    val fixes = map cert xs;
wenzelm@28340
   113
wenzelm@28340
   114
    val tfrees = fold Term.add_tfrees (prop :: As) [];
wenzelm@28340
   115
    val instT = map (fn (a, S) => (certT (TVar ((a, 0), S)), certT (TFree (a, S)))) tfrees;
wenzelm@28340
   116
wenzelm@28340
   117
    val global_prop =
wenzelm@28340
   118
      Term.map_types Logic.varifyT (fold_rev Logic.all xs (Logic.list_implies (As, prop)));
wenzelm@28340
   119
    val global_result =
wenzelm@28340
   120
      Thm.generalize (map #1 tfrees, []) 0 o
wenzelm@28340
   121
      Drule.forall_intr_list fixes o
wenzelm@28340
   122
      Drule.implies_intr_list assms o
wenzelm@28340
   123
      Thm.adjust_maxidx_thm ~1 o result;
wenzelm@28340
   124
    val local_result =
wenzelm@28340
   125
      Thm.promise (Future.fork_irrelevant global_result) (cert global_prop)
wenzelm@28340
   126
      |> Thm.instantiate (instT, [])
wenzelm@28340
   127
      |> Drule.forall_elim_list fixes
wenzelm@28340
   128
      |> fold (Thm.elim_implies o Thm.assume) assms;
wenzelm@28340
   129
  in local_result end;
wenzelm@28340
   130
wenzelm@28340
   131
wenzelm@18027
   132
wenzelm@18027
   133
(** tactical theorem proving **)
wenzelm@18027
   134
wenzelm@23356
   135
(* prove_internal -- minimal checks, no normalization of result! *)
wenzelm@20250
   136
wenzelm@23356
   137
fun prove_internal casms cprop tac =
wenzelm@20250
   138
  (case SINGLE (tac (map Assumption.assume casms)) (init cprop) of
wenzelm@20250
   139
    SOME th => Drule.implies_intr_list casms (finish th)
wenzelm@20250
   140
  | NONE => error "Tactic failed.");
wenzelm@20250
   141
wenzelm@20250
   142
wenzelm@28340
   143
(* prove_common etc. *)
wenzelm@17986
   144
wenzelm@28340
   145
fun prove_common immediate ctxt xs asms props tac =
wenzelm@17980
   146
  let
wenzelm@21516
   147
    val thy = ProofContext.theory_of ctxt;
wenzelm@26939
   148
    val string_of_term = Syntax.string_of_term ctxt;
wenzelm@20056
   149
wenzelm@20250
   150
    fun err msg = cat_error msg
wenzelm@20250
   151
      ("The error(s) above occurred for the goal statement:\n" ^
wenzelm@20250
   152
        string_of_term (Logic.list_implies (asms, Logic.mk_conjunction_list props)));
wenzelm@17980
   153
wenzelm@20250
   154
    fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm (Term.no_dummy_patterns t))
wenzelm@17980
   155
      handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
wenzelm@20250
   156
    val casms = map cert_safe asms;
wenzelm@20250
   157
    val cprops = map cert_safe props;
wenzelm@17980
   158
wenzelm@20250
   159
    val (prems, ctxt') = ctxt
wenzelm@20250
   160
      |> Variable.add_fixes_direct xs
wenzelm@27218
   161
      |> fold Variable.declare_term (asms @ props)
wenzelm@26713
   162
      |> Assumption.add_assumes casms
wenzelm@26713
   163
      ||> Variable.set_body true;
wenzelm@17980
   164
wenzelm@28340
   165
    val stmt = Conjunction.mk_conjunction_balanced cprops;
wenzelm@28340
   166
wenzelm@28340
   167
    fun result () =
wenzelm@28340
   168
      (case SINGLE (tac {prems = prems, context = ctxt'}) (init stmt) of
wenzelm@19774
   169
        NONE => err "Tactic failed."
wenzelm@28340
   170
      | SOME st =>
wenzelm@28340
   171
          let val res = finish st handle THM (msg, _, _) => err msg in
wenzelm@28340
   172
            if Unify.matches_list thy [Thm.term_of stmt] [Thm.prop_of res] then res
wenzelm@28340
   173
            else err ("Proved a different theorem: " ^ string_of_term (Thm.prop_of res))
wenzelm@28340
   174
          end);
wenzelm@28340
   175
    val res =
wenzelm@28340
   176
      if immediate orelse #maxidx (Thm.rep_cterm stmt) >= 0 then result ()
wenzelm@28340
   177
      else promise_result ctxt result (Thm.term_of stmt);
wenzelm@17980
   178
  in
wenzelm@28340
   179
    Conjunction.elim_balanced (length props) res
wenzelm@20290
   180
    |> map (Assumption.export false ctxt' ctxt)
wenzelm@20056
   181
    |> Variable.export ctxt' ctxt
wenzelm@20250
   182
    |> map Drule.zero_var_indexes
wenzelm@17980
   183
  end;
wenzelm@17980
   184
wenzelm@28340
   185
val prove_multi = prove_common false;
wenzelm@17980
   186
wenzelm@28340
   187
fun prove_promise ctxt xs asms prop tac = hd (prove_common false ctxt xs asms [prop] tac);
wenzelm@28340
   188
fun prove ctxt xs asms prop tac = hd (prove_common true ctxt xs asms [prop] tac);
wenzelm@20056
   189
wenzelm@20056
   190
fun prove_global thy xs asms prop tac =
wenzelm@26713
   191
  Drule.standard (prove (ProofContext.init thy) xs asms prop tac);
wenzelm@18027
   192
wenzelm@18027
   193
wenzelm@17980
   194
wenzelm@21687
   195
(** goal structure **)
wenzelm@21687
   196
wenzelm@21687
   197
(* nested goals *)
wenzelm@18207
   198
wenzelm@19184
   199
fun extract i n st =
wenzelm@19184
   200
  (if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st then Seq.empty
wenzelm@19184
   201
   else if n = 1 then Seq.single (Thm.cprem_of st i)
wenzelm@23418
   202
   else
wenzelm@23418
   203
     Seq.single (Conjunction.mk_conjunction_balanced (map (Thm.cprem_of st) (i upto i + n - 1))))
wenzelm@20260
   204
  |> Seq.map (Thm.adjust_maxidx_cterm ~1 #> init);
wenzelm@17980
   205
wenzelm@19221
   206
fun retrofit i n st' st =
wenzelm@19221
   207
  (if n = 1 then st
wenzelm@23418
   208
   else st |> Drule.with_subgoal i (Conjunction.uncurry_balanced n))
wenzelm@19221
   209
  |> Thm.compose_no_flatten false (conclude st', Thm.nprems_of st') i;
wenzelm@18207
   210
wenzelm@17980
   211
fun SELECT_GOAL tac i st =
wenzelm@19191
   212
  if Thm.nprems_of st = 1 andalso i = 1 then tac st
wenzelm@19184
   213
  else Seq.lifts (retrofit i 1) (Seq.maps tac (extract i 1 st)) st;
wenzelm@17980
   214
wenzelm@21687
   215
wenzelm@21687
   216
(* multiple goals *)
wenzelm@21687
   217
wenzelm@23418
   218
fun precise_conjunction_tac 0 i = eq_assume_tac i
wenzelm@23418
   219
  | precise_conjunction_tac 1 i = SUBGOAL (K all_tac) i
wenzelm@23418
   220
  | precise_conjunction_tac n i = PRIMITIVE (Drule.with_subgoal i (Conjunction.curry_balanced n));
wenzelm@23414
   221
wenzelm@23418
   222
val adhoc_conjunction_tac = REPEAT_ALL_NEW
wenzelm@23418
   223
  (SUBGOAL (fn (goal, i) =>
wenzelm@23418
   224
    if can Logic.dest_conjunction goal then rtac Conjunction.conjunctionI i
wenzelm@23418
   225
    else no_tac));
wenzelm@21687
   226
wenzelm@23418
   227
val conjunction_tac = SUBGOAL (fn (goal, i) =>
wenzelm@23418
   228
  precise_conjunction_tac (length (Logic.dest_conjunctions goal)) i ORELSE
wenzelm@23418
   229
  TRY (adhoc_conjunction_tac i));
wenzelm@21687
   230
wenzelm@23418
   231
val recover_conjunction_tac = PRIMITIVE (fn th =>
wenzelm@23418
   232
  Conjunction.uncurry_balanced (Thm.nprems_of th) th);
wenzelm@23414
   233
wenzelm@23414
   234
fun PRECISE_CONJUNCTS n tac =
wenzelm@23414
   235
  SELECT_GOAL (precise_conjunction_tac n 1
wenzelm@23414
   236
    THEN tac
wenzelm@23418
   237
    THEN recover_conjunction_tac);
wenzelm@23414
   238
wenzelm@21687
   239
fun CONJUNCTS tac =
wenzelm@21687
   240
  SELECT_GOAL (conjunction_tac 1
wenzelm@21687
   241
    THEN tac
wenzelm@23418
   242
    THEN recover_conjunction_tac);
wenzelm@21687
   243
wenzelm@21687
   244
wenzelm@21687
   245
(* hhf normal form *)
wenzelm@21687
   246
wenzelm@21687
   247
val norm_hhf_tac =
wenzelm@21687
   248
  rtac Drule.asm_rl  (*cheap approximation -- thanks to builtin Logic.flatten_params*)
wenzelm@21687
   249
  THEN' SUBGOAL (fn (t, i) =>
wenzelm@21687
   250
    if Drule.is_norm_hhf t then all_tac
wenzelm@23536
   251
    else MetaSimplifier.rewrite_goal_tac [Drule.norm_hhf_eq] i);
wenzelm@21687
   252
wenzelm@25301
   253
fun compose_hhf_tac th i st =
wenzelm@25301
   254
  PRIMSEQ (Thm.bicompose false (false, Drule.lift_all (Thm.cprem_of st i) th, 0) i) st;
wenzelm@21687
   255
wenzelm@23237
   256
wenzelm@23237
   257
(* non-atomic goal assumptions *)
wenzelm@23237
   258
wenzelm@23356
   259
fun non_atomic (Const ("==>", _) $ _ $ _) = true
wenzelm@23356
   260
  | non_atomic (Const ("all", _) $ _) = true
wenzelm@23356
   261
  | non_atomic _ = false;
wenzelm@23356
   262
wenzelm@23237
   263
fun assume_rule_tac ctxt = norm_hhf_tac THEN' CSUBGOAL (fn (goal, i) =>
wenzelm@23237
   264
  let
wenzelm@23237
   265
    val ((_, goal'), ctxt') = Variable.focus goal ctxt;
wenzelm@23237
   266
    val goal'' = Drule.cterm_rule (singleton (Variable.export ctxt' ctxt)) goal';
wenzelm@23356
   267
    val Rs = filter (non_atomic o Thm.term_of) (Drule.strip_imp_prems goal'');
wenzelm@23237
   268
    val tacs = Rs |> map (fn R =>
wenzelm@23237
   269
      Tactic.etac (MetaSimplifier.norm_hhf (Thm.trivial R)) THEN_ALL_NEW assume_tac);
wenzelm@23237
   270
  in fold_rev (curry op APPEND') tacs (K no_tac) i end);
wenzelm@23237
   271
wenzelm@18207
   272
end;
wenzelm@18207
   273
wenzelm@17980
   274
structure BasicGoal: BASIC_GOAL = Goal;
wenzelm@17980
   275
open BasicGoal;