src/Pure/goal.ML
author wenzelm
Sun Jul 19 19:24:04 2009 +0200 (2009-07-19)
changeset 32061 11f8ee55662d
parent 32058 c76fd93b3b99
child 32062 457f5bcd3d76
permissions -rw-r--r--
parallel_proofs: more fine-grained control with optional parallel checking of nested Isar proofs;
wenzelm@17980
     1
(*  Title:      Pure/goal.ML
wenzelm@29345
     2
    Author:     Makarius
wenzelm@17980
     3
wenzelm@18139
     4
Goals in tactical theorem proving.
wenzelm@17980
     5
*)
wenzelm@17980
     6
wenzelm@17980
     7
signature BASIC_GOAL =
wenzelm@17980
     8
sig
wenzelm@32061
     9
  val parallel_proofs: int ref
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@29448
    23
  val future_enabled: unit -> bool
wenzelm@32061
    24
  val local_future_enabled: unit -> bool
wenzelm@28973
    25
  val future_result: Proof.context -> thm future -> term -> thm
wenzelm@23356
    26
  val prove_internal: cterm list -> cterm -> (thm list -> tactic) -> thm
wenzelm@20290
    27
  val prove_multi: Proof.context -> string list -> term list -> term list ->
wenzelm@20290
    28
    ({prems: thm list, context: Proof.context} -> tactic) -> thm list
wenzelm@28446
    29
  val prove_future: Proof.context -> string list -> term list -> term ->
wenzelm@28340
    30
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@20290
    31
  val prove: Proof.context -> string list -> term list -> term ->
wenzelm@20290
    32
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@26713
    33
  val prove_global: theory -> string list -> term list -> term ->
wenzelm@26713
    34
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@19184
    35
  val extract: int -> int -> thm -> thm Seq.seq
wenzelm@19184
    36
  val retrofit: int -> int -> thm -> thm -> thm Seq.seq
wenzelm@23418
    37
  val conjunction_tac: int -> tactic
wenzelm@23414
    38
  val precise_conjunction_tac: int -> int -> tactic
wenzelm@23418
    39
  val recover_conjunction_tac: tactic
wenzelm@21687
    40
  val norm_hhf_tac: int -> tactic
wenzelm@21687
    41
  val compose_hhf_tac: thm -> int -> tactic
wenzelm@23237
    42
  val assume_rule_tac: Proof.context -> int -> tactic
wenzelm@17980
    43
end;
wenzelm@17980
    44
wenzelm@17980
    45
structure Goal: GOAL =
wenzelm@17980
    46
struct
wenzelm@17980
    47
wenzelm@18027
    48
(** goals **)
wenzelm@18027
    49
wenzelm@18027
    50
(*
wenzelm@18027
    51
  -------- (init)
wenzelm@18027
    52
  C ==> #C
wenzelm@18027
    53
*)
wenzelm@20290
    54
val init =
wenzelm@22902
    55
  let val A = #1 (Thm.dest_implies (Thm.cprop_of Drule.protectI))
wenzelm@20290
    56
  in fn C => Thm.instantiate ([], [(A, C)]) Drule.protectI end;
wenzelm@17980
    57
wenzelm@17980
    58
(*
wenzelm@18119
    59
   C
wenzelm@18119
    60
  --- (protect)
wenzelm@18119
    61
  #C
wenzelm@17980
    62
*)
wenzelm@29345
    63
fun protect th = Drule.comp_no_flatten (th, 0) 1 Drule.protectI;
wenzelm@17980
    64
wenzelm@17980
    65
(*
wenzelm@18027
    66
  A ==> ... ==> #C
wenzelm@18027
    67
  ---------------- (conclude)
wenzelm@17980
    68
  A ==> ... ==> C
wenzelm@17980
    69
*)
wenzelm@29345
    70
fun conclude th = Drule.comp_no_flatten (th, Thm.nprems_of th) 1 Drule.protectD;
wenzelm@17980
    71
wenzelm@17980
    72
(*
wenzelm@18027
    73
  #C
wenzelm@18027
    74
  --- (finish)
wenzelm@18027
    75
   C
wenzelm@17983
    76
*)
wenzelm@17980
    77
fun finish th =
wenzelm@17980
    78
  (case Thm.nprems_of th of
wenzelm@17980
    79
    0 => conclude th
wenzelm@17980
    80
  | n => raise THM ("Proof failed.\n" ^
wenzelm@17980
    81
      Pretty.string_of (Pretty.chunks (Display.pretty_goals n th)) ^
wenzelm@17980
    82
      ("\n" ^ string_of_int n ^ " unsolved goal(s)!"), 0, [th]));
wenzelm@17980
    83
wenzelm@17980
    84
wenzelm@18027
    85
wenzelm@18027
    86
(** results **)
wenzelm@18027
    87
wenzelm@28340
    88
(* normal form *)
wenzelm@28340
    89
wenzelm@21604
    90
val norm_result =
wenzelm@21604
    91
  Drule.flexflex_unique
wenzelm@21604
    92
  #> MetaSimplifier.norm_hhf_protect
wenzelm@21604
    93
  #> Thm.strip_shyps
wenzelm@21604
    94
  #> Drule.zero_var_indexes;
wenzelm@21604
    95
wenzelm@21604
    96
wenzelm@29448
    97
(* future_enabled *)
wenzelm@29448
    98
wenzelm@32061
    99
val parallel_proofs = ref 1;
wenzelm@29448
   100
wenzelm@29448
   101
fun future_enabled () =
wenzelm@32061
   102
  Future.enabled () andalso Future.is_worker () andalso ! parallel_proofs >= 1;
wenzelm@32061
   103
wenzelm@32061
   104
fun local_future_enabled () = future_enabled () andalso ! parallel_proofs >= 2;
wenzelm@29448
   105
wenzelm@29448
   106
wenzelm@28446
   107
(* future_result *)
wenzelm@28340
   108
wenzelm@28446
   109
fun future_result ctxt result prop =
wenzelm@28340
   110
  let
wenzelm@28340
   111
    val thy = ProofContext.theory_of ctxt;
wenzelm@28340
   112
    val _ = Context.reject_draft thy;
wenzelm@28340
   113
    val cert = Thm.cterm_of thy;
wenzelm@28340
   114
    val certT = Thm.ctyp_of thy;
wenzelm@28340
   115
wenzelm@30473
   116
    val assms = Assumption.all_assms_of ctxt;
wenzelm@28340
   117
    val As = map Thm.term_of assms;
wenzelm@28340
   118
wenzelm@28340
   119
    val xs = map Free (fold Term.add_frees (prop :: As) []);
wenzelm@28340
   120
    val fixes = map cert xs;
wenzelm@28340
   121
wenzelm@28340
   122
    val tfrees = fold Term.add_tfrees (prop :: As) [];
wenzelm@28340
   123
    val instT = map (fn (a, S) => (certT (TVar ((a, 0), S)), certT (TFree (a, S)))) tfrees;
wenzelm@28340
   124
wenzelm@28340
   125
    val global_prop =
wenzelm@32056
   126
      cert (Term.map_types Logic.varifyT (fold_rev Logic.all xs (Logic.list_implies (As, prop))))
wenzelm@32056
   127
      |> Thm.weaken_sorts (Variable.sorts_of ctxt);
wenzelm@28973
   128
    val global_result = result |> Future.map
wenzelm@28973
   129
      (Thm.adjust_maxidx_thm ~1 #>
wenzelm@28973
   130
        Drule.implies_intr_list assms #>
wenzelm@28973
   131
        Drule.forall_intr_list fixes #>
wenzelm@28973
   132
        Thm.generalize (map #1 tfrees, []) 0);
wenzelm@28340
   133
    val local_result =
wenzelm@32056
   134
      Thm.future global_result global_prop
wenzelm@28340
   135
      |> Thm.instantiate (instT, [])
wenzelm@28340
   136
      |> Drule.forall_elim_list fixes
wenzelm@28340
   137
      |> fold (Thm.elim_implies o Thm.assume) assms;
wenzelm@28340
   138
  in local_result end;
wenzelm@28340
   139
wenzelm@28340
   140
wenzelm@18027
   141
wenzelm@18027
   142
(** tactical theorem proving **)
wenzelm@18027
   143
wenzelm@23356
   144
(* prove_internal -- minimal checks, no normalization of result! *)
wenzelm@20250
   145
wenzelm@23356
   146
fun prove_internal casms cprop tac =
wenzelm@20250
   147
  (case SINGLE (tac (map Assumption.assume casms)) (init cprop) of
wenzelm@20250
   148
    SOME th => Drule.implies_intr_list casms (finish th)
wenzelm@20250
   149
  | NONE => error "Tactic failed.");
wenzelm@20250
   150
wenzelm@20250
   151
wenzelm@28340
   152
(* prove_common etc. *)
wenzelm@17986
   153
wenzelm@28340
   154
fun prove_common immediate ctxt xs asms props tac =
wenzelm@17980
   155
  let
wenzelm@21516
   156
    val thy = ProofContext.theory_of ctxt;
wenzelm@26939
   157
    val string_of_term = Syntax.string_of_term ctxt;
wenzelm@20056
   158
wenzelm@28355
   159
    val pos = Position.thread_data ();
wenzelm@20250
   160
    fun err msg = cat_error msg
wenzelm@20250
   161
      ("The error(s) above occurred for the goal statement:\n" ^
wenzelm@28355
   162
        string_of_term (Logic.list_implies (asms, Logic.mk_conjunction_list props)) ^
wenzelm@28355
   163
        (case Position.str_of pos of "" => "" | s => "\n" ^ s));
wenzelm@17980
   164
wenzelm@20250
   165
    fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm (Term.no_dummy_patterns t))
wenzelm@17980
   166
      handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
wenzelm@20250
   167
    val casms = map cert_safe asms;
wenzelm@20250
   168
    val cprops = map cert_safe props;
wenzelm@17980
   169
wenzelm@20250
   170
    val (prems, ctxt') = ctxt
wenzelm@20250
   171
      |> Variable.add_fixes_direct xs
wenzelm@27218
   172
      |> fold Variable.declare_term (asms @ props)
wenzelm@26713
   173
      |> Assumption.add_assumes casms
wenzelm@26713
   174
      ||> Variable.set_body true;
wenzelm@28619
   175
    val sorts = Variable.sorts_of ctxt';
wenzelm@17980
   176
wenzelm@28619
   177
    val stmt = Thm.weaken_sorts sorts (Conjunction.mk_conjunction_balanced cprops);
wenzelm@28340
   178
wenzelm@28340
   179
    fun result () =
wenzelm@28340
   180
      (case SINGLE (tac {prems = prems, context = ctxt'}) (init stmt) of
wenzelm@19774
   181
        NONE => err "Tactic failed."
wenzelm@28340
   182
      | SOME st =>
wenzelm@28340
   183
          let val res = finish st handle THM (msg, _, _) => err msg in
wenzelm@28619
   184
            if Unify.matches_list thy [Thm.term_of stmt] [Thm.prop_of res]
wenzelm@28619
   185
            then Thm.check_shyps sorts res
wenzelm@28340
   186
            else err ("Proved a different theorem: " ^ string_of_term (Thm.prop_of res))
wenzelm@28340
   187
          end);
wenzelm@28340
   188
    val res =
wenzelm@29448
   189
      if immediate orelse #maxidx (Thm.rep_cterm stmt) >= 0 orelse not (future_enabled ())
wenzelm@29088
   190
      then result ()
wenzelm@32058
   191
      else future_result ctxt' (Future.fork_local ~1 result) (Thm.term_of stmt);
wenzelm@17980
   192
  in
wenzelm@28340
   193
    Conjunction.elim_balanced (length props) res
wenzelm@20290
   194
    |> map (Assumption.export false ctxt' ctxt)
wenzelm@20056
   195
    |> Variable.export ctxt' ctxt
wenzelm@20250
   196
    |> map Drule.zero_var_indexes
wenzelm@17980
   197
  end;
wenzelm@17980
   198
wenzelm@28341
   199
val prove_multi = prove_common true;
wenzelm@17980
   200
wenzelm@28446
   201
fun prove_future ctxt xs asms prop tac = hd (prove_common false ctxt xs asms [prop] tac);
wenzelm@28340
   202
fun prove ctxt xs asms prop tac = hd (prove_common true ctxt xs asms [prop] tac);
wenzelm@20056
   203
wenzelm@20056
   204
fun prove_global thy xs asms prop tac =
wenzelm@26713
   205
  Drule.standard (prove (ProofContext.init thy) xs asms prop tac);
wenzelm@18027
   206
wenzelm@18027
   207
wenzelm@17980
   208
wenzelm@21687
   209
(** goal structure **)
wenzelm@21687
   210
wenzelm@21687
   211
(* nested goals *)
wenzelm@18207
   212
wenzelm@19184
   213
fun extract i n st =
wenzelm@19184
   214
  (if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st then Seq.empty
wenzelm@19184
   215
   else if n = 1 then Seq.single (Thm.cprem_of st i)
wenzelm@23418
   216
   else
wenzelm@23418
   217
     Seq.single (Conjunction.mk_conjunction_balanced (map (Thm.cprem_of st) (i upto i + n - 1))))
wenzelm@20260
   218
  |> Seq.map (Thm.adjust_maxidx_cterm ~1 #> init);
wenzelm@17980
   219
wenzelm@19221
   220
fun retrofit i n st' st =
wenzelm@19221
   221
  (if n = 1 then st
wenzelm@23418
   222
   else st |> Drule.with_subgoal i (Conjunction.uncurry_balanced n))
wenzelm@19221
   223
  |> Thm.compose_no_flatten false (conclude st', Thm.nprems_of st') i;
wenzelm@18207
   224
wenzelm@17980
   225
fun SELECT_GOAL tac i st =
wenzelm@19191
   226
  if Thm.nprems_of st = 1 andalso i = 1 then tac st
wenzelm@19184
   227
  else Seq.lifts (retrofit i 1) (Seq.maps tac (extract i 1 st)) st;
wenzelm@17980
   228
wenzelm@21687
   229
wenzelm@21687
   230
(* multiple goals *)
wenzelm@21687
   231
wenzelm@23418
   232
fun precise_conjunction_tac 0 i = eq_assume_tac i
wenzelm@23418
   233
  | precise_conjunction_tac 1 i = SUBGOAL (K all_tac) i
wenzelm@23418
   234
  | precise_conjunction_tac n i = PRIMITIVE (Drule.with_subgoal i (Conjunction.curry_balanced n));
wenzelm@23414
   235
wenzelm@23418
   236
val adhoc_conjunction_tac = REPEAT_ALL_NEW
wenzelm@23418
   237
  (SUBGOAL (fn (goal, i) =>
wenzelm@23418
   238
    if can Logic.dest_conjunction goal then rtac Conjunction.conjunctionI i
wenzelm@23418
   239
    else no_tac));
wenzelm@21687
   240
wenzelm@23418
   241
val conjunction_tac = SUBGOAL (fn (goal, i) =>
wenzelm@23418
   242
  precise_conjunction_tac (length (Logic.dest_conjunctions goal)) i ORELSE
wenzelm@23418
   243
  TRY (adhoc_conjunction_tac i));
wenzelm@21687
   244
wenzelm@23418
   245
val recover_conjunction_tac = PRIMITIVE (fn th =>
wenzelm@23418
   246
  Conjunction.uncurry_balanced (Thm.nprems_of th) th);
wenzelm@23414
   247
wenzelm@23414
   248
fun PRECISE_CONJUNCTS n tac =
wenzelm@23414
   249
  SELECT_GOAL (precise_conjunction_tac n 1
wenzelm@23414
   250
    THEN tac
wenzelm@23418
   251
    THEN recover_conjunction_tac);
wenzelm@23414
   252
wenzelm@21687
   253
fun CONJUNCTS tac =
wenzelm@21687
   254
  SELECT_GOAL (conjunction_tac 1
wenzelm@21687
   255
    THEN tac
wenzelm@23418
   256
    THEN recover_conjunction_tac);
wenzelm@21687
   257
wenzelm@21687
   258
wenzelm@21687
   259
(* hhf normal form *)
wenzelm@21687
   260
wenzelm@21687
   261
val norm_hhf_tac =
wenzelm@21687
   262
  rtac Drule.asm_rl  (*cheap approximation -- thanks to builtin Logic.flatten_params*)
wenzelm@21687
   263
  THEN' SUBGOAL (fn (t, i) =>
wenzelm@21687
   264
    if Drule.is_norm_hhf t then all_tac
wenzelm@28619
   265
    else MetaSimplifier.rewrite_goal_tac Drule.norm_hhf_eqs i);
wenzelm@21687
   266
wenzelm@25301
   267
fun compose_hhf_tac th i st =
wenzelm@25301
   268
  PRIMSEQ (Thm.bicompose false (false, Drule.lift_all (Thm.cprem_of st i) th, 0) i) st;
wenzelm@21687
   269
wenzelm@23237
   270
wenzelm@23237
   271
(* non-atomic goal assumptions *)
wenzelm@23237
   272
wenzelm@23356
   273
fun non_atomic (Const ("==>", _) $ _ $ _) = true
wenzelm@23356
   274
  | non_atomic (Const ("all", _) $ _) = true
wenzelm@23356
   275
  | non_atomic _ = false;
wenzelm@23356
   276
wenzelm@23237
   277
fun assume_rule_tac ctxt = norm_hhf_tac THEN' CSUBGOAL (fn (goal, i) =>
wenzelm@23237
   278
  let
wenzelm@23237
   279
    val ((_, goal'), ctxt') = Variable.focus goal ctxt;
wenzelm@23237
   280
    val goal'' = Drule.cterm_rule (singleton (Variable.export ctxt' ctxt)) goal';
wenzelm@23356
   281
    val Rs = filter (non_atomic o Thm.term_of) (Drule.strip_imp_prems goal'');
wenzelm@23237
   282
    val tacs = Rs |> map (fn R =>
wenzelm@23237
   283
      Tactic.etac (MetaSimplifier.norm_hhf (Thm.trivial R)) THEN_ALL_NEW assume_tac);
wenzelm@23237
   284
  in fold_rev (curry op APPEND') tacs (K no_tac) i end);
wenzelm@23237
   285
wenzelm@18207
   286
end;
wenzelm@18207
   287
wenzelm@17980
   288
structure BasicGoal: BASIC_GOAL = Goal;
wenzelm@17980
   289
open BasicGoal;