src/Pure/goal.ML
author wenzelm
Fri Mar 06 17:32:20 2015 +0100 (2015-03-06)
changeset 59623 920889b0788e
parent 59621 291934bac95e
child 60642 48dd1cefb4ae
permissions -rw-r--r--
clarified context;
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@30473
   130
    val assms = Assumption.all_assms_of ctxt;
wenzelm@28340
   131
    val As = map Thm.term_of assms;
wenzelm@28340
   132
wenzelm@28340
   133
    val xs = map Free (fold Term.add_frees (prop :: As) []);
wenzelm@59623
   134
    val fixes = map (Thm.cterm_of ctxt) xs;
wenzelm@28340
   135
wenzelm@28340
   136
    val tfrees = fold Term.add_tfrees (prop :: As) [];
wenzelm@59616
   137
    val instT =
wenzelm@59623
   138
      map (fn (a, S) => apply2 (Thm.ctyp_of ctxt) (TVar ((a, 0), S), TFree (a, S))) tfrees;
wenzelm@28340
   139
wenzelm@28340
   140
    val global_prop =
wenzelm@59616
   141
      Logic.varify_types_global (fold_rev Logic.all xs (Logic.list_implies (As, prop)))
wenzelm@59623
   142
      |> Thm.cterm_of ctxt
wenzelm@32056
   143
      |> Thm.weaken_sorts (Variable.sorts_of ctxt);
wenzelm@28973
   144
    val global_result = result |> Future.map
wenzelm@58950
   145
      (Drule.flexflex_unique (SOME ctxt) #>
wenzelm@33768
   146
        Thm.adjust_maxidx_thm ~1 #>
wenzelm@28973
   147
        Drule.implies_intr_list assms #>
wenzelm@28973
   148
        Drule.forall_intr_list fixes #>
wenzelm@36613
   149
        Thm.generalize (map #1 tfrees, []) 0 #>
wenzelm@36613
   150
        Thm.strip_shyps);
wenzelm@28340
   151
    val local_result =
wenzelm@32056
   152
      Thm.future global_result global_prop
wenzelm@50987
   153
      |> Thm.close_derivation
wenzelm@28340
   154
      |> Thm.instantiate (instT, [])
wenzelm@28340
   155
      |> Drule.forall_elim_list fixes
wenzelm@28340
   156
      |> fold (Thm.elim_implies o Thm.assume) assms;
wenzelm@28340
   157
  in local_result end;
wenzelm@28340
   158
wenzelm@28340
   159
wenzelm@18027
   160
wenzelm@18027
   161
(** tactical theorem proving **)
wenzelm@18027
   162
wenzelm@23356
   163
(* prove_internal -- minimal checks, no normalization of result! *)
wenzelm@20250
   164
wenzelm@54883
   165
fun prove_internal ctxt casms cprop tac =
wenzelm@54883
   166
  (case SINGLE (tac (map (Assumption.assume ctxt) casms)) (init cprop) of
wenzelm@54992
   167
    SOME th => Drule.implies_intr_list casms (finish ctxt th)
wenzelm@38875
   168
  | NONE => error "Tactic failed");
wenzelm@20250
   169
wenzelm@20250
   170
wenzelm@51553
   171
(* prove variations *)
wenzelm@51553
   172
wenzelm@51553
   173
fun is_schematic t =
wenzelm@51553
   174
  Term.exists_subterm Term.is_Var t orelse
wenzelm@51553
   175
  Term.exists_type (Term.exists_subtype Term.is_TVar) t;
wenzelm@17986
   176
wenzelm@59564
   177
fun prove_common ctxt fork_pri xs asms props tac =
wenzelm@17980
   178
  let
wenzelm@42360
   179
    val thy = Proof_Context.theory_of ctxt;
wenzelm@20056
   180
wenzelm@51553
   181
    val schematic = exists is_schematic props;
wenzelm@59564
   182
    val immediate = is_none fork_pri;
wenzelm@53189
   183
    val future = future_enabled 1;
wenzelm@52499
   184
    val skip = not immediate andalso not schematic andalso future andalso skip_proofs_enabled ();
wenzelm@51553
   185
wenzelm@28355
   186
    val pos = Position.thread_data ();
wenzelm@55559
   187
    fun err msg =
wenzelm@55559
   188
      cat_error msg
wenzelm@55559
   189
        ("The error(s) above occurred for the goal statement" ^ Position.here pos ^ ":\n" ^
wenzelm@55559
   190
          Syntax.string_of_term ctxt (Logic.list_implies (asms, Logic.mk_conjunction_list props)));
wenzelm@17980
   191
wenzelm@59623
   192
    fun cert_safe t = Thm.cterm_of ctxt (Envir.beta_norm (Term.no_dummy_patterns t))
wenzelm@17980
   193
      handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
wenzelm@20250
   194
    val casms = map cert_safe asms;
wenzelm@20250
   195
    val cprops = map cert_safe props;
wenzelm@17980
   196
wenzelm@20250
   197
    val (prems, ctxt') = ctxt
wenzelm@20250
   198
      |> Variable.add_fixes_direct xs
wenzelm@27218
   199
      |> fold Variable.declare_term (asms @ props)
wenzelm@26713
   200
      |> Assumption.add_assumes casms
wenzelm@26713
   201
      ||> Variable.set_body true;
wenzelm@28619
   202
    val sorts = Variable.sorts_of ctxt';
wenzelm@17980
   203
wenzelm@28619
   204
    val stmt = Thm.weaken_sorts sorts (Conjunction.mk_conjunction_balanced cprops);
wenzelm@28340
   205
wenzelm@51553
   206
    fun tac' args st =
wenzelm@59498
   207
      if skip then ALLGOALS (Skip_Proof.cheat_tac ctxt) st before Skip_Proof.report ctxt
wenzelm@51553
   208
      else tac args st;
wenzelm@28340
   209
    fun result () =
wenzelm@51553
   210
      (case SINGLE (tac' {prems = prems, context = ctxt'}) (init stmt) of
wenzelm@38875
   211
        NONE => err "Tactic failed"
wenzelm@28340
   212
      | SOME st =>
wenzelm@54567
   213
          let
wenzelm@54981
   214
            val _ =
wenzelm@59582
   215
              Theory.subthy (Thm.theory_of_thm st, thy) orelse
wenzelm@59582
   216
                err "Bad background theory of goal state";
wenzelm@54567
   217
            val res =
wenzelm@54567
   218
              (finish ctxt' st
wenzelm@58950
   219
                |> Drule.flexflex_unique (SOME ctxt')
wenzelm@54567
   220
                |> Thm.check_shyps sorts
wenzelm@54994
   221
                |> Thm.check_hyps (Context.Proof ctxt'))
wenzelm@54567
   222
              handle THM (msg, _, _) => err msg | ERROR msg => err msg;
wenzelm@54567
   223
          in
wenzelm@58950
   224
            if Unify.matches_list (Context.Proof ctxt') [Thm.term_of stmt] [Thm.prop_of res]
wenzelm@58950
   225
            then res
wenzelm@54567
   226
            else err ("Proved a different theorem: " ^ Syntax.string_of_term ctxt' (Thm.prop_of res))
wenzelm@28340
   227
          end);
wenzelm@28340
   228
    val res =
wenzelm@53192
   229
      if immediate orelse schematic orelse not future orelse skip then result ()
wenzelm@53192
   230
      else
wenzelm@53192
   231
        future_result ctxt'
wenzelm@59564
   232
          (Execution.fork {name = "Goal.prove", pos = Position.thread_data (), pri = the fork_pri}
wenzelm@59564
   233
            result)
wenzelm@53192
   234
          (Thm.term_of stmt);
wenzelm@17980
   235
  in
wenzelm@28340
   236
    Conjunction.elim_balanced (length props) res
wenzelm@20290
   237
    |> map (Assumption.export false ctxt' ctxt)
wenzelm@20056
   238
    |> Variable.export ctxt' ctxt
wenzelm@20250
   239
    |> map Drule.zero_var_indexes
wenzelm@17980
   240
  end;
wenzelm@17980
   241
wenzelm@59564
   242
fun prove_future_pri ctxt pri xs asms prop tac =
wenzelm@59564
   243
  hd (prove_common ctxt (SOME pri) xs asms [prop] tac);
wenzelm@17980
   244
wenzelm@59564
   245
fun prove_future ctxt = prove_future_pri ctxt ~1;
wenzelm@51118
   246
wenzelm@59564
   247
fun prove ctxt xs asms prop tac = hd (prove_common ctxt NONE xs asms [prop] tac);
wenzelm@20056
   248
wenzelm@51118
   249
fun prove_global_future thy xs asms prop tac =
wenzelm@51118
   250
  Drule.export_without_context (prove_future (Proof_Context.init_global thy) xs asms prop tac);
wenzelm@51118
   251
wenzelm@20056
   252
fun prove_global thy xs asms prop tac =
wenzelm@42360
   253
  Drule.export_without_context (prove (Proof_Context.init_global thy) xs asms prop tac);
wenzelm@18027
   254
wenzelm@51551
   255
fun prove_sorry ctxt xs asms prop tac =
wenzelm@52059
   256
  if Config.get ctxt quick_and_dirty then
wenzelm@59498
   257
    prove ctxt xs asms prop (fn _ => ALLGOALS (Skip_Proof.cheat_tac ctxt))
wenzelm@59564
   258
  else (if future_enabled 1 then prove_future_pri ctxt ~2 else prove ctxt) xs asms prop tac;
wenzelm@51551
   259
wenzelm@51551
   260
fun prove_sorry_global thy xs asms prop tac =
wenzelm@51551
   261
  Drule.export_without_context
wenzelm@51551
   262
    (prove_sorry (Proof_Context.init_global thy) xs asms prop tac);
wenzelm@51551
   263
wenzelm@18027
   264
wenzelm@17980
   265
wenzelm@21687
   266
(** goal structure **)
wenzelm@21687
   267
wenzelm@52458
   268
(* rearrange subgoals *)
wenzelm@52458
   269
wenzelm@52458
   270
fun restrict i n st =
wenzelm@52458
   271
  if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st
wenzelm@52458
   272
  then raise THM ("Goal.restrict", i, [st])
wenzelm@52458
   273
  else rotate_prems (i - 1) st |> protect n;
wenzelm@52458
   274
wenzelm@52458
   275
fun unrestrict i = conclude #> rotate_prems (1 - i);
wenzelm@52458
   276
wenzelm@52458
   277
(*with structural marker*)
wenzelm@17980
   278
fun SELECT_GOAL tac i st =
wenzelm@19191
   279
  if Thm.nprems_of st = 1 andalso i = 1 then tac st
wenzelm@52458
   280
  else (PRIMITIVE (restrict i 1) THEN tac THEN PRIMITIVE (unrestrict i)) st;
wenzelm@52458
   281
wenzelm@52458
   282
(*without structural marker*)
wenzelm@52458
   283
fun PREFER_GOAL tac i st =
wenzelm@52458
   284
  if i < 1 orelse i > Thm.nprems_of st then Seq.empty
wenzelm@52458
   285
  else (PRIMITIVE (rotate_prems (i - 1)) THEN tac THEN PRIMITIVE (rotate_prems (1 - i))) st;
wenzelm@17980
   286
wenzelm@21687
   287
wenzelm@21687
   288
(* multiple goals *)
wenzelm@21687
   289
wenzelm@23418
   290
fun precise_conjunction_tac 0 i = eq_assume_tac i
wenzelm@23418
   291
  | precise_conjunction_tac 1 i = SUBGOAL (K all_tac) i
wenzelm@23418
   292
  | precise_conjunction_tac n i = PRIMITIVE (Drule.with_subgoal i (Conjunction.curry_balanced n));
wenzelm@23414
   293
wenzelm@23418
   294
val adhoc_conjunction_tac = REPEAT_ALL_NEW
wenzelm@23418
   295
  (SUBGOAL (fn (goal, i) =>
wenzelm@59498
   296
    if can Logic.dest_conjunction goal then resolve0_tac [Conjunction.conjunctionI] i
wenzelm@23418
   297
    else no_tac));
wenzelm@21687
   298
wenzelm@23418
   299
val conjunction_tac = SUBGOAL (fn (goal, i) =>
wenzelm@23418
   300
  precise_conjunction_tac (length (Logic.dest_conjunctions goal)) i ORELSE
wenzelm@23418
   301
  TRY (adhoc_conjunction_tac i));
wenzelm@21687
   302
wenzelm@23418
   303
val recover_conjunction_tac = PRIMITIVE (fn th =>
wenzelm@23418
   304
  Conjunction.uncurry_balanced (Thm.nprems_of th) th);
wenzelm@23414
   305
wenzelm@23414
   306
fun PRECISE_CONJUNCTS n tac =
wenzelm@23414
   307
  SELECT_GOAL (precise_conjunction_tac n 1
wenzelm@23414
   308
    THEN tac
wenzelm@23418
   309
    THEN recover_conjunction_tac);
wenzelm@23414
   310
wenzelm@21687
   311
fun CONJUNCTS tac =
wenzelm@21687
   312
  SELECT_GOAL (conjunction_tac 1
wenzelm@21687
   313
    THEN tac
wenzelm@23418
   314
    THEN recover_conjunction_tac);
wenzelm@21687
   315
wenzelm@21687
   316
wenzelm@21687
   317
(* hhf normal form *)
wenzelm@21687
   318
wenzelm@54742
   319
fun norm_hhf_tac ctxt =
wenzelm@59498
   320
  resolve_tac ctxt [Drule.asm_rl]  (*cheap approximation -- thanks to builtin Logic.flatten_params*)
wenzelm@21687
   321
  THEN' SUBGOAL (fn (t, i) =>
wenzelm@21687
   322
    if Drule.is_norm_hhf t then all_tac
wenzelm@54742
   323
    else rewrite_goal_tac ctxt Drule.norm_hhf_eqs i);
wenzelm@21687
   324
wenzelm@23237
   325
wenzelm@23237
   326
(* non-atomic goal assumptions *)
wenzelm@23237
   327
wenzelm@56245
   328
fun non_atomic (Const ("Pure.imp", _) $ _ $ _) = true
wenzelm@56245
   329
  | non_atomic (Const ("Pure.all", _) $ _) = true
wenzelm@23356
   330
  | non_atomic _ = false;
wenzelm@23356
   331
wenzelm@54742
   332
fun assume_rule_tac ctxt = norm_hhf_tac ctxt THEN' CSUBGOAL (fn (goal, i) =>
wenzelm@23237
   333
  let
wenzelm@42495
   334
    val ((_, goal'), ctxt') = Variable.focus_cterm goal ctxt;
wenzelm@23237
   335
    val goal'' = Drule.cterm_rule (singleton (Variable.export ctxt' ctxt)) goal';
wenzelm@23356
   336
    val Rs = filter (non_atomic o Thm.term_of) (Drule.strip_imp_prems goal'');
wenzelm@23237
   337
    val tacs = Rs |> map (fn R =>
wenzelm@59498
   338
      eresolve_tac ctxt [Raw_Simplifier.norm_hhf ctxt (Thm.trivial R)] THEN_ALL_NEW assume_tac ctxt);
wenzelm@23237
   339
  in fold_rev (curry op APPEND') tacs (K no_tac) i end);
wenzelm@23237
   340
wenzelm@52461
   341
end;
wenzelm@52461
   342
wenzelm@32365
   343
structure Basic_Goal: BASIC_GOAL = Goal;
wenzelm@32365
   344
open Basic_Goal;