src/Pure/goal.ML
author wenzelm
Tue Dec 16 18:04:16 2008 +0100 (2008-12-16)
changeset 29124 ce6f21913e54
parent 29122 b3bae49a013a
child 29343 43ac99cdeb5b
permissions -rw-r--r--
future proofs: Future.fork_pri 1 minimizes queue length and pending promises
-- slight improvement of throughput, at the cost of a bit of parallelism;
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@28973
    23
  val future_result: Proof.context -> thm future -> 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@28446
    27
  val prove_future: 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@28446
    99
(* future_result *)
wenzelm@28340
   100
wenzelm@28446
   101
fun future_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@28973
   119
    val global_result = result |> Future.map
wenzelm@28973
   120
      (Thm.adjust_maxidx_thm ~1 #>
wenzelm@28973
   121
        Drule.implies_intr_list assms #>
wenzelm@28973
   122
        Drule.forall_intr_list fixes #>
wenzelm@28973
   123
        Thm.generalize (map #1 tfrees, []) 0);
wenzelm@28340
   124
    val local_result =
wenzelm@28446
   125
      Thm.future 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@28355
   150
    val pos = Position.thread_data ();
wenzelm@20250
   151
    fun err msg = cat_error msg
wenzelm@20250
   152
      ("The error(s) above occurred for the goal statement:\n" ^
wenzelm@28355
   153
        string_of_term (Logic.list_implies (asms, Logic.mk_conjunction_list props)) ^
wenzelm@28355
   154
        (case Position.str_of pos of "" => "" | s => "\n" ^ s));
wenzelm@17980
   155
wenzelm@20250
   156
    fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm (Term.no_dummy_patterns t))
wenzelm@17980
   157
      handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
wenzelm@20250
   158
    val casms = map cert_safe asms;
wenzelm@20250
   159
    val cprops = map cert_safe props;
wenzelm@17980
   160
wenzelm@20250
   161
    val (prems, ctxt') = ctxt
wenzelm@20250
   162
      |> Variable.add_fixes_direct xs
wenzelm@27218
   163
      |> fold Variable.declare_term (asms @ props)
wenzelm@26713
   164
      |> Assumption.add_assumes casms
wenzelm@26713
   165
      ||> Variable.set_body true;
wenzelm@28619
   166
    val sorts = Variable.sorts_of ctxt';
wenzelm@17980
   167
wenzelm@28619
   168
    val stmt = Thm.weaken_sorts sorts (Conjunction.mk_conjunction_balanced cprops);
wenzelm@28340
   169
wenzelm@28340
   170
    fun result () =
wenzelm@28340
   171
      (case SINGLE (tac {prems = prems, context = ctxt'}) (init stmt) of
wenzelm@19774
   172
        NONE => err "Tactic failed."
wenzelm@28340
   173
      | SOME st =>
wenzelm@28340
   174
          let val res = finish st handle THM (msg, _, _) => err msg in
wenzelm@28619
   175
            if Unify.matches_list thy [Thm.term_of stmt] [Thm.prop_of res]
wenzelm@28619
   176
            then Thm.check_shyps sorts res
wenzelm@28340
   177
            else err ("Proved a different theorem: " ^ string_of_term (Thm.prop_of res))
wenzelm@28340
   178
          end);
wenzelm@28340
   179
    val res =
wenzelm@29088
   180
      if immediate orelse #maxidx (Thm.rep_cterm stmt) >= 0 orelse not (Future.enabled ())
wenzelm@29088
   181
      then result ()
wenzelm@29124
   182
      else future_result ctxt' (Future.fork_pri 1 result) (Thm.term_of stmt);
wenzelm@17980
   183
  in
wenzelm@28340
   184
    Conjunction.elim_balanced (length props) res
wenzelm@20290
   185
    |> map (Assumption.export false ctxt' ctxt)
wenzelm@20056
   186
    |> Variable.export ctxt' ctxt
wenzelm@20250
   187
    |> map Drule.zero_var_indexes
wenzelm@17980
   188
  end;
wenzelm@17980
   189
wenzelm@28341
   190
val prove_multi = prove_common true;
wenzelm@17980
   191
wenzelm@28446
   192
fun prove_future ctxt xs asms prop tac = hd (prove_common false ctxt xs asms [prop] tac);
wenzelm@28340
   193
fun prove ctxt xs asms prop tac = hd (prove_common true ctxt xs asms [prop] tac);
wenzelm@20056
   194
wenzelm@20056
   195
fun prove_global thy xs asms prop tac =
wenzelm@26713
   196
  Drule.standard (prove (ProofContext.init thy) xs asms prop tac);
wenzelm@18027
   197
wenzelm@18027
   198
wenzelm@17980
   199
wenzelm@21687
   200
(** goal structure **)
wenzelm@21687
   201
wenzelm@21687
   202
(* nested goals *)
wenzelm@18207
   203
wenzelm@19184
   204
fun extract i n st =
wenzelm@19184
   205
  (if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st then Seq.empty
wenzelm@19184
   206
   else if n = 1 then Seq.single (Thm.cprem_of st i)
wenzelm@23418
   207
   else
wenzelm@23418
   208
     Seq.single (Conjunction.mk_conjunction_balanced (map (Thm.cprem_of st) (i upto i + n - 1))))
wenzelm@20260
   209
  |> Seq.map (Thm.adjust_maxidx_cterm ~1 #> init);
wenzelm@17980
   210
wenzelm@19221
   211
fun retrofit i n st' st =
wenzelm@19221
   212
  (if n = 1 then st
wenzelm@23418
   213
   else st |> Drule.with_subgoal i (Conjunction.uncurry_balanced n))
wenzelm@19221
   214
  |> Thm.compose_no_flatten false (conclude st', Thm.nprems_of st') i;
wenzelm@18207
   215
wenzelm@17980
   216
fun SELECT_GOAL tac i st =
wenzelm@19191
   217
  if Thm.nprems_of st = 1 andalso i = 1 then tac st
wenzelm@19184
   218
  else Seq.lifts (retrofit i 1) (Seq.maps tac (extract i 1 st)) st;
wenzelm@17980
   219
wenzelm@21687
   220
wenzelm@21687
   221
(* multiple goals *)
wenzelm@21687
   222
wenzelm@23418
   223
fun precise_conjunction_tac 0 i = eq_assume_tac i
wenzelm@23418
   224
  | precise_conjunction_tac 1 i = SUBGOAL (K all_tac) i
wenzelm@23418
   225
  | precise_conjunction_tac n i = PRIMITIVE (Drule.with_subgoal i (Conjunction.curry_balanced n));
wenzelm@23414
   226
wenzelm@23418
   227
val adhoc_conjunction_tac = REPEAT_ALL_NEW
wenzelm@23418
   228
  (SUBGOAL (fn (goal, i) =>
wenzelm@23418
   229
    if can Logic.dest_conjunction goal then rtac Conjunction.conjunctionI i
wenzelm@23418
   230
    else no_tac));
wenzelm@21687
   231
wenzelm@23418
   232
val conjunction_tac = SUBGOAL (fn (goal, i) =>
wenzelm@23418
   233
  precise_conjunction_tac (length (Logic.dest_conjunctions goal)) i ORELSE
wenzelm@23418
   234
  TRY (adhoc_conjunction_tac i));
wenzelm@21687
   235
wenzelm@23418
   236
val recover_conjunction_tac = PRIMITIVE (fn th =>
wenzelm@23418
   237
  Conjunction.uncurry_balanced (Thm.nprems_of th) th);
wenzelm@23414
   238
wenzelm@23414
   239
fun PRECISE_CONJUNCTS n tac =
wenzelm@23414
   240
  SELECT_GOAL (precise_conjunction_tac n 1
wenzelm@23414
   241
    THEN tac
wenzelm@23418
   242
    THEN recover_conjunction_tac);
wenzelm@23414
   243
wenzelm@21687
   244
fun CONJUNCTS tac =
wenzelm@21687
   245
  SELECT_GOAL (conjunction_tac 1
wenzelm@21687
   246
    THEN tac
wenzelm@23418
   247
    THEN recover_conjunction_tac);
wenzelm@21687
   248
wenzelm@21687
   249
wenzelm@21687
   250
(* hhf normal form *)
wenzelm@21687
   251
wenzelm@21687
   252
val norm_hhf_tac =
wenzelm@21687
   253
  rtac Drule.asm_rl  (*cheap approximation -- thanks to builtin Logic.flatten_params*)
wenzelm@21687
   254
  THEN' SUBGOAL (fn (t, i) =>
wenzelm@21687
   255
    if Drule.is_norm_hhf t then all_tac
wenzelm@28619
   256
    else MetaSimplifier.rewrite_goal_tac Drule.norm_hhf_eqs i);
wenzelm@21687
   257
wenzelm@25301
   258
fun compose_hhf_tac th i st =
wenzelm@25301
   259
  PRIMSEQ (Thm.bicompose false (false, Drule.lift_all (Thm.cprem_of st i) th, 0) i) st;
wenzelm@21687
   260
wenzelm@23237
   261
wenzelm@23237
   262
(* non-atomic goal assumptions *)
wenzelm@23237
   263
wenzelm@23356
   264
fun non_atomic (Const ("==>", _) $ _ $ _) = true
wenzelm@23356
   265
  | non_atomic (Const ("all", _) $ _) = true
wenzelm@23356
   266
  | non_atomic _ = false;
wenzelm@23356
   267
wenzelm@23237
   268
fun assume_rule_tac ctxt = norm_hhf_tac THEN' CSUBGOAL (fn (goal, i) =>
wenzelm@23237
   269
  let
wenzelm@23237
   270
    val ((_, goal'), ctxt') = Variable.focus goal ctxt;
wenzelm@23237
   271
    val goal'' = Drule.cterm_rule (singleton (Variable.export ctxt' ctxt)) goal';
wenzelm@23356
   272
    val Rs = filter (non_atomic o Thm.term_of) (Drule.strip_imp_prems goal'');
wenzelm@23237
   273
    val tacs = Rs |> map (fn R =>
wenzelm@23237
   274
      Tactic.etac (MetaSimplifier.norm_hhf (Thm.trivial R)) THEN_ALL_NEW assume_tac);
wenzelm@23237
   275
  in fold_rev (curry op APPEND') tacs (K no_tac) i end);
wenzelm@23237
   276
wenzelm@18207
   277
end;
wenzelm@18207
   278
wenzelm@17980
   279
structure BasicGoal: BASIC_GOAL = Goal;
wenzelm@17980
   280
open BasicGoal;