src/Pure/goal.ML
author wenzelm
Mon Feb 23 14:50:30 2015 +0100 (2015-02-23)
changeset 59564 fdc03c8daacc
parent 59498 50b60f501b05
child 59582 0fbed69ff081
permissions -rw-r--r--
Goal.prove_multi is superseded by the fully general Goal.prove_common;
wenzelm@17980
     1
(*  Title:      Pure/goal.ML
wenzelm@29345
     2
    Author:     Makarius
wenzelm@17980
     3
wenzelm@49061
     4
Goals in tactical theorem proving, with support for forked proofs.
wenzelm@17980
     5
*)
wenzelm@17980
     6
wenzelm@17980
     7
signature BASIC_GOAL =
wenzelm@17980
     8
sig
wenzelm@32738
     9
  val parallel_proofs: int Unsynchronized.ref
wenzelm@17980
    10
  val SELECT_GOAL: tactic -> int -> tactic
wenzelm@52458
    11
  val PREFER_GOAL: tactic -> int -> tactic
wenzelm@23418
    12
  val CONJUNCTS: tactic -> int -> tactic
wenzelm@23414
    13
  val PRECISE_CONJUNCTS: int -> tactic -> int -> tactic
wenzelm@17980
    14
end;
wenzelm@17980
    15
wenzelm@17980
    16
signature GOAL =
wenzelm@17980
    17
sig
wenzelm@17980
    18
  include BASIC_GOAL
wenzelm@17980
    19
  val init: cterm -> thm
wenzelm@52456
    20
  val protect: int -> thm -> thm
wenzelm@17980
    21
  val conclude: thm -> thm
wenzelm@49845
    22
  val check_finished: Proof.context -> thm -> thm
wenzelm@32197
    23
  val finish: Proof.context -> thm -> thm
wenzelm@54883
    24
  val norm_result: Proof.context -> thm -> thm
wenzelm@52499
    25
  val skip_proofs_enabled: unit -> bool
wenzelm@53189
    26
  val future_enabled: int -> bool
wenzelm@51423
    27
  val future_enabled_timing: Time.time -> bool
wenzelm@28973
    28
  val future_result: Proof.context -> thm future -> term -> thm
wenzelm@54883
    29
  val prove_internal: Proof.context -> cterm list -> cterm -> (thm list -> tactic) -> thm
wenzelm@51553
    30
  val is_schematic: term -> bool
wenzelm@59564
    31
  val prove_common: Proof.context -> int option -> string list -> term list -> term list ->
wenzelm@20290
    32
    ({prems: thm list, context: Proof.context} -> tactic) -> thm list
wenzelm@28446
    33
  val prove_future: Proof.context -> string list -> term list -> term ->
wenzelm@28340
    34
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@20290
    35
  val prove: Proof.context -> string list -> term list -> term ->
wenzelm@20290
    36
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@51118
    37
  val prove_global_future: theory -> string list -> term list -> term ->
wenzelm@51118
    38
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@26713
    39
  val prove_global: theory -> string list -> term list -> term ->
wenzelm@26713
    40
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@51551
    41
  val prove_sorry: Proof.context -> string list -> term list -> term ->
wenzelm@51551
    42
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@51551
    43
  val prove_sorry_global: theory -> string list -> term list -> term ->
wenzelm@51551
    44
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@52458
    45
  val restrict: int -> int -> thm -> thm
wenzelm@52458
    46
  val unrestrict: int -> thm -> thm
wenzelm@23418
    47
  val conjunction_tac: int -> tactic
wenzelm@23414
    48
  val precise_conjunction_tac: int -> int -> tactic
wenzelm@23418
    49
  val recover_conjunction_tac: tactic
wenzelm@54742
    50
  val norm_hhf_tac: Proof.context -> int -> tactic
wenzelm@23237
    51
  val assume_rule_tac: Proof.context -> int -> tactic
wenzelm@17980
    52
end;
wenzelm@17980
    53
wenzelm@17980
    54
structure Goal: GOAL =
wenzelm@17980
    55
struct
wenzelm@17980
    56
wenzelm@18027
    57
(** goals **)
wenzelm@18027
    58
wenzelm@18027
    59
(*
wenzelm@18027
    60
  -------- (init)
wenzelm@18027
    61
  C ==> #C
wenzelm@18027
    62
*)
wenzelm@20290
    63
val init =
wenzelm@22902
    64
  let val A = #1 (Thm.dest_implies (Thm.cprop_of Drule.protectI))
wenzelm@20290
    65
  in fn C => Thm.instantiate ([], [(A, C)]) Drule.protectI end;
wenzelm@17980
    66
wenzelm@17980
    67
(*
wenzelm@52456
    68
  A1 ==> ... ==> An ==> C
wenzelm@52456
    69
  ------------------------ (protect n)
wenzelm@52456
    70
  A1 ==> ... ==> An ==> #C
wenzelm@17980
    71
*)
wenzelm@52456
    72
fun protect n th = Drule.comp_no_flatten (th, n) 1 Drule.protectI;
wenzelm@17980
    73
wenzelm@17980
    74
(*
wenzelm@18027
    75
  A ==> ... ==> #C
wenzelm@18027
    76
  ---------------- (conclude)
wenzelm@17980
    77
  A ==> ... ==> C
wenzelm@17980
    78
*)
wenzelm@29345
    79
fun conclude th = Drule.comp_no_flatten (th, Thm.nprems_of th) 1 Drule.protectD;
wenzelm@17980
    80
wenzelm@17980
    81
(*
wenzelm@18027
    82
  #C
wenzelm@18027
    83
  --- (finish)
wenzelm@18027
    84
   C
wenzelm@17983
    85
*)
wenzelm@32197
    86
fun check_finished ctxt th =
wenzelm@51608
    87
  if Thm.no_prems th then th
wenzelm@51608
    88
  else
wenzelm@51958
    89
    raise THM ("Proof failed.\n" ^ Pretty.string_of (Goal_Display.pretty_goal ctxt th), 0, [th]);
wenzelm@32197
    90
wenzelm@49845
    91
fun finish ctxt = check_finished ctxt #> conclude;
wenzelm@17980
    92
wenzelm@17980
    93
wenzelm@18027
    94
wenzelm@18027
    95
(** results **)
wenzelm@18027
    96
wenzelm@28340
    97
(* normal form *)
wenzelm@28340
    98
wenzelm@54883
    99
fun norm_result ctxt =
wenzelm@58950
   100
  Drule.flexflex_unique (SOME ctxt)
wenzelm@54883
   101
  #> Raw_Simplifier.norm_hhf_protect ctxt
wenzelm@21604
   102
  #> Thm.strip_shyps
wenzelm@21604
   103
  #> Drule.zero_var_indexes;
wenzelm@21604
   104
wenzelm@21604
   105
wenzelm@41703
   106
(* scheduling parameters *)
wenzelm@41703
   107
wenzelm@52499
   108
fun skip_proofs_enabled () =
wenzelm@52710
   109
  let val skip = Options.default_bool "skip_proofs" in
wenzelm@52499
   110
    if Proofterm.proofs_enabled () andalso skip then
wenzelm@52499
   111
      (warning "Proof terms enabled -- cannot skip proofs"; false)
wenzelm@52499
   112
    else skip
wenzelm@52499
   113
  end;
wenzelm@52499
   114
wenzelm@32738
   115
val parallel_proofs = Unsynchronized.ref 1;
wenzelm@29448
   116
wenzelm@53189
   117
fun future_enabled n =
wenzelm@49012
   118
  Multithreading.enabled () andalso ! parallel_proofs >= n andalso
wenzelm@41703
   119
  is_some (Future.worker_task ());
wenzelm@32061
   120
wenzelm@51423
   121
fun future_enabled_timing t =
wenzelm@53189
   122
  future_enabled 1 andalso
wenzelm@52104
   123
    Time.toReal t >= Options.default_real "parallel_subproofs_threshold";
wenzelm@29448
   124
wenzelm@29448
   125
wenzelm@28446
   126
(* future_result *)
wenzelm@28340
   127
wenzelm@28446
   128
fun future_result ctxt result prop =
wenzelm@28340
   129
  let
wenzelm@42360
   130
    val thy = Proof_Context.theory_of ctxt;
wenzelm@28340
   131
    val cert = Thm.cterm_of thy;
wenzelm@28340
   132
    val certT = Thm.ctyp_of thy;
wenzelm@28340
   133
wenzelm@30473
   134
    val assms = Assumption.all_assms_of ctxt;
wenzelm@28340
   135
    val As = map Thm.term_of assms;
wenzelm@28340
   136
wenzelm@28340
   137
    val xs = map Free (fold Term.add_frees (prop :: As) []);
wenzelm@28340
   138
    val fixes = map cert xs;
wenzelm@28340
   139
wenzelm@28340
   140
    val tfrees = fold Term.add_tfrees (prop :: As) [];
wenzelm@28340
   141
    val instT = map (fn (a, S) => (certT (TVar ((a, 0), S)), certT (TFree (a, S)))) tfrees;
wenzelm@28340
   142
wenzelm@28340
   143
    val global_prop =
wenzelm@45344
   144
      cert (Logic.varify_types_global (fold_rev Logic.all xs (Logic.list_implies (As, prop))))
wenzelm@32056
   145
      |> Thm.weaken_sorts (Variable.sorts_of ctxt);
wenzelm@28973
   146
    val global_result = result |> Future.map
wenzelm@58950
   147
      (Drule.flexflex_unique (SOME ctxt) #>
wenzelm@33768
   148
        Thm.adjust_maxidx_thm ~1 #>
wenzelm@28973
   149
        Drule.implies_intr_list assms #>
wenzelm@28973
   150
        Drule.forall_intr_list fixes #>
wenzelm@36613
   151
        Thm.generalize (map #1 tfrees, []) 0 #>
wenzelm@36613
   152
        Thm.strip_shyps);
wenzelm@28340
   153
    val local_result =
wenzelm@32056
   154
      Thm.future global_result global_prop
wenzelm@50987
   155
      |> Thm.close_derivation
wenzelm@28340
   156
      |> Thm.instantiate (instT, [])
wenzelm@28340
   157
      |> Drule.forall_elim_list fixes
wenzelm@28340
   158
      |> fold (Thm.elim_implies o Thm.assume) assms;
wenzelm@28340
   159
  in local_result end;
wenzelm@28340
   160
wenzelm@28340
   161
wenzelm@18027
   162
wenzelm@18027
   163
(** tactical theorem proving **)
wenzelm@18027
   164
wenzelm@23356
   165
(* prove_internal -- minimal checks, no normalization of result! *)
wenzelm@20250
   166
wenzelm@54883
   167
fun prove_internal ctxt casms cprop tac =
wenzelm@54883
   168
  (case SINGLE (tac (map (Assumption.assume ctxt) casms)) (init cprop) of
wenzelm@54992
   169
    SOME th => Drule.implies_intr_list casms (finish ctxt th)
wenzelm@38875
   170
  | NONE => error "Tactic failed");
wenzelm@20250
   171
wenzelm@20250
   172
wenzelm@51553
   173
(* prove variations *)
wenzelm@51553
   174
wenzelm@51553
   175
fun is_schematic t =
wenzelm@51553
   176
  Term.exists_subterm Term.is_Var t orelse
wenzelm@51553
   177
  Term.exists_type (Term.exists_subtype Term.is_TVar) t;
wenzelm@17986
   178
wenzelm@59564
   179
fun prove_common ctxt fork_pri xs asms props tac =
wenzelm@17980
   180
  let
wenzelm@42360
   181
    val thy = Proof_Context.theory_of ctxt;
wenzelm@20056
   182
wenzelm@51553
   183
    val schematic = exists is_schematic props;
wenzelm@59564
   184
    val immediate = is_none fork_pri;
wenzelm@53189
   185
    val future = future_enabled 1;
wenzelm@52499
   186
    val skip = not immediate andalso not schematic andalso future andalso skip_proofs_enabled ();
wenzelm@51553
   187
wenzelm@28355
   188
    val pos = Position.thread_data ();
wenzelm@55559
   189
    fun err msg =
wenzelm@55559
   190
      cat_error msg
wenzelm@55559
   191
        ("The error(s) above occurred for the goal statement" ^ Position.here pos ^ ":\n" ^
wenzelm@55559
   192
          Syntax.string_of_term ctxt (Logic.list_implies (asms, Logic.mk_conjunction_list props)));
wenzelm@17980
   193
wenzelm@20250
   194
    fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm (Term.no_dummy_patterns t))
wenzelm@17980
   195
      handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
wenzelm@20250
   196
    val casms = map cert_safe asms;
wenzelm@20250
   197
    val cprops = map cert_safe props;
wenzelm@17980
   198
wenzelm@20250
   199
    val (prems, ctxt') = ctxt
wenzelm@20250
   200
      |> Variable.add_fixes_direct xs
wenzelm@27218
   201
      |> fold Variable.declare_term (asms @ props)
wenzelm@26713
   202
      |> Assumption.add_assumes casms
wenzelm@26713
   203
      ||> Variable.set_body true;
wenzelm@28619
   204
    val sorts = Variable.sorts_of ctxt';
wenzelm@17980
   205
wenzelm@28619
   206
    val stmt = Thm.weaken_sorts sorts (Conjunction.mk_conjunction_balanced cprops);
wenzelm@28340
   207
wenzelm@51553
   208
    fun tac' args st =
wenzelm@59498
   209
      if skip then ALLGOALS (Skip_Proof.cheat_tac ctxt) st before Skip_Proof.report ctxt
wenzelm@51553
   210
      else tac args st;
wenzelm@28340
   211
    fun result () =
wenzelm@51553
   212
      (case SINGLE (tac' {prems = prems, context = ctxt'}) (init stmt) of
wenzelm@38875
   213
        NONE => err "Tactic failed"
wenzelm@28340
   214
      | SOME st =>
wenzelm@54567
   215
          let
wenzelm@54981
   216
            val _ =
wenzelm@54981
   217
              Theory.subthy (theory_of_thm st, thy) orelse err "Bad background theory of goal state";
wenzelm@54567
   218
            val res =
wenzelm@54567
   219
              (finish ctxt' st
wenzelm@58950
   220
                |> Drule.flexflex_unique (SOME ctxt')
wenzelm@54567
   221
                |> Thm.check_shyps sorts
wenzelm@54994
   222
                |> Thm.check_hyps (Context.Proof ctxt'))
wenzelm@54567
   223
              handle THM (msg, _, _) => err msg | ERROR msg => err msg;
wenzelm@54567
   224
          in
wenzelm@58950
   225
            if Unify.matches_list (Context.Proof ctxt') [Thm.term_of stmt] [Thm.prop_of res]
wenzelm@58950
   226
            then res
wenzelm@54567
   227
            else err ("Proved a different theorem: " ^ Syntax.string_of_term ctxt' (Thm.prop_of res))
wenzelm@28340
   228
          end);
wenzelm@28340
   229
    val res =
wenzelm@53192
   230
      if immediate orelse schematic orelse not future orelse skip then result ()
wenzelm@53192
   231
      else
wenzelm@53192
   232
        future_result ctxt'
wenzelm@59564
   233
          (Execution.fork {name = "Goal.prove", pos = Position.thread_data (), pri = the fork_pri}
wenzelm@59564
   234
            result)
wenzelm@53192
   235
          (Thm.term_of stmt);
wenzelm@17980
   236
  in
wenzelm@28340
   237
    Conjunction.elim_balanced (length props) res
wenzelm@20290
   238
    |> map (Assumption.export false ctxt' ctxt)
wenzelm@20056
   239
    |> Variable.export ctxt' ctxt
wenzelm@20250
   240
    |> map Drule.zero_var_indexes
wenzelm@17980
   241
  end;
wenzelm@17980
   242
wenzelm@59564
   243
fun prove_future_pri ctxt pri xs asms prop tac =
wenzelm@59564
   244
  hd (prove_common ctxt (SOME pri) xs asms [prop] tac);
wenzelm@17980
   245
wenzelm@59564
   246
fun prove_future ctxt = prove_future_pri ctxt ~1;
wenzelm@51118
   247
wenzelm@59564
   248
fun prove ctxt xs asms prop tac = hd (prove_common ctxt NONE xs asms [prop] tac);
wenzelm@20056
   249
wenzelm@51118
   250
fun prove_global_future thy xs asms prop tac =
wenzelm@51118
   251
  Drule.export_without_context (prove_future (Proof_Context.init_global thy) xs asms prop tac);
wenzelm@51118
   252
wenzelm@20056
   253
fun prove_global thy xs asms prop tac =
wenzelm@42360
   254
  Drule.export_without_context (prove (Proof_Context.init_global thy) xs asms prop tac);
wenzelm@18027
   255
wenzelm@51551
   256
fun prove_sorry ctxt xs asms prop tac =
wenzelm@52059
   257
  if Config.get ctxt quick_and_dirty then
wenzelm@59498
   258
    prove ctxt xs asms prop (fn _ => ALLGOALS (Skip_Proof.cheat_tac ctxt))
wenzelm@59564
   259
  else (if future_enabled 1 then prove_future_pri ctxt ~2 else prove ctxt) xs asms prop tac;
wenzelm@51551
   260
wenzelm@51551
   261
fun prove_sorry_global thy xs asms prop tac =
wenzelm@51551
   262
  Drule.export_without_context
wenzelm@51551
   263
    (prove_sorry (Proof_Context.init_global thy) xs asms prop tac);
wenzelm@51551
   264
wenzelm@18027
   265
wenzelm@17980
   266
wenzelm@21687
   267
(** goal structure **)
wenzelm@21687
   268
wenzelm@52458
   269
(* rearrange subgoals *)
wenzelm@52458
   270
wenzelm@52458
   271
fun restrict i n st =
wenzelm@52458
   272
  if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st
wenzelm@52458
   273
  then raise THM ("Goal.restrict", i, [st])
wenzelm@52458
   274
  else rotate_prems (i - 1) st |> protect n;
wenzelm@52458
   275
wenzelm@52458
   276
fun unrestrict i = conclude #> rotate_prems (1 - i);
wenzelm@52458
   277
wenzelm@52458
   278
(*with structural marker*)
wenzelm@17980
   279
fun SELECT_GOAL tac i st =
wenzelm@19191
   280
  if Thm.nprems_of st = 1 andalso i = 1 then tac st
wenzelm@52458
   281
  else (PRIMITIVE (restrict i 1) THEN tac THEN PRIMITIVE (unrestrict i)) st;
wenzelm@52458
   282
wenzelm@52458
   283
(*without structural marker*)
wenzelm@52458
   284
fun PREFER_GOAL tac i st =
wenzelm@52458
   285
  if i < 1 orelse i > Thm.nprems_of st then Seq.empty
wenzelm@52458
   286
  else (PRIMITIVE (rotate_prems (i - 1)) THEN tac THEN PRIMITIVE (rotate_prems (1 - i))) st;
wenzelm@17980
   287
wenzelm@21687
   288
wenzelm@21687
   289
(* multiple goals *)
wenzelm@21687
   290
wenzelm@23418
   291
fun precise_conjunction_tac 0 i = eq_assume_tac i
wenzelm@23418
   292
  | precise_conjunction_tac 1 i = SUBGOAL (K all_tac) i
wenzelm@23418
   293
  | precise_conjunction_tac n i = PRIMITIVE (Drule.with_subgoal i (Conjunction.curry_balanced n));
wenzelm@23414
   294
wenzelm@23418
   295
val adhoc_conjunction_tac = REPEAT_ALL_NEW
wenzelm@23418
   296
  (SUBGOAL (fn (goal, i) =>
wenzelm@59498
   297
    if can Logic.dest_conjunction goal then resolve0_tac [Conjunction.conjunctionI] i
wenzelm@23418
   298
    else no_tac));
wenzelm@21687
   299
wenzelm@23418
   300
val conjunction_tac = SUBGOAL (fn (goal, i) =>
wenzelm@23418
   301
  precise_conjunction_tac (length (Logic.dest_conjunctions goal)) i ORELSE
wenzelm@23418
   302
  TRY (adhoc_conjunction_tac i));
wenzelm@21687
   303
wenzelm@23418
   304
val recover_conjunction_tac = PRIMITIVE (fn th =>
wenzelm@23418
   305
  Conjunction.uncurry_balanced (Thm.nprems_of th) th);
wenzelm@23414
   306
wenzelm@23414
   307
fun PRECISE_CONJUNCTS n tac =
wenzelm@23414
   308
  SELECT_GOAL (precise_conjunction_tac n 1
wenzelm@23414
   309
    THEN tac
wenzelm@23418
   310
    THEN recover_conjunction_tac);
wenzelm@23414
   311
wenzelm@21687
   312
fun CONJUNCTS tac =
wenzelm@21687
   313
  SELECT_GOAL (conjunction_tac 1
wenzelm@21687
   314
    THEN tac
wenzelm@23418
   315
    THEN recover_conjunction_tac);
wenzelm@21687
   316
wenzelm@21687
   317
wenzelm@21687
   318
(* hhf normal form *)
wenzelm@21687
   319
wenzelm@54742
   320
fun norm_hhf_tac ctxt =
wenzelm@59498
   321
  resolve_tac ctxt [Drule.asm_rl]  (*cheap approximation -- thanks to builtin Logic.flatten_params*)
wenzelm@21687
   322
  THEN' SUBGOAL (fn (t, i) =>
wenzelm@21687
   323
    if Drule.is_norm_hhf t then all_tac
wenzelm@54742
   324
    else rewrite_goal_tac ctxt Drule.norm_hhf_eqs i);
wenzelm@21687
   325
wenzelm@23237
   326
wenzelm@23237
   327
(* non-atomic goal assumptions *)
wenzelm@23237
   328
wenzelm@56245
   329
fun non_atomic (Const ("Pure.imp", _) $ _ $ _) = true
wenzelm@56245
   330
  | non_atomic (Const ("Pure.all", _) $ _) = true
wenzelm@23356
   331
  | non_atomic _ = false;
wenzelm@23356
   332
wenzelm@54742
   333
fun assume_rule_tac ctxt = norm_hhf_tac ctxt THEN' CSUBGOAL (fn (goal, i) =>
wenzelm@23237
   334
  let
wenzelm@42495
   335
    val ((_, goal'), ctxt') = Variable.focus_cterm goal ctxt;
wenzelm@23237
   336
    val goal'' = Drule.cterm_rule (singleton (Variable.export ctxt' ctxt)) goal';
wenzelm@23356
   337
    val Rs = filter (non_atomic o Thm.term_of) (Drule.strip_imp_prems goal'');
wenzelm@23237
   338
    val tacs = Rs |> map (fn R =>
wenzelm@59498
   339
      eresolve_tac ctxt [Raw_Simplifier.norm_hhf ctxt (Thm.trivial R)] THEN_ALL_NEW assume_tac ctxt);
wenzelm@23237
   340
  in fold_rev (curry op APPEND') tacs (K no_tac) i end);
wenzelm@23237
   341
wenzelm@52461
   342
end;
wenzelm@52461
   343
wenzelm@32365
   344
structure Basic_Goal: BASIC_GOAL = Goal;
wenzelm@32365
   345
open Basic_Goal;