src/Pure/goal.ML
author wenzelm
Wed Mar 27 14:50:30 2013 +0100 (2013-03-27)
changeset 51552 c713c9505f68
parent 51551 88d1d19fb74f
child 51553 63327f679cff
permissions -rw-r--r--
clarified Skip_Proof.cheat_tac: more standard tactic;
clarified Method.cheating: check quick_and_dirty when it is actually applied;
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@51423
    10
  val parallel_subproofs_saturation: int Unsynchronized.ref
wenzelm@51423
    11
  val parallel_subproofs_threshold: real Unsynchronized.ref
wenzelm@17980
    12
  val SELECT_GOAL: tactic -> int -> tactic
wenzelm@23418
    13
  val CONJUNCTS: tactic -> int -> tactic
wenzelm@23414
    14
  val PRECISE_CONJUNCTS: int -> tactic -> int -> tactic
wenzelm@32365
    15
  val PARALLEL_CHOICE: tactic list -> tactic
wenzelm@32365
    16
  val PARALLEL_GOALS: tactic -> tactic
wenzelm@17980
    17
end;
wenzelm@17980
    18
wenzelm@17980
    19
signature GOAL =
wenzelm@17980
    20
sig
wenzelm@17980
    21
  include BASIC_GOAL
wenzelm@17980
    22
  val init: cterm -> thm
wenzelm@18027
    23
  val protect: thm -> thm
wenzelm@17980
    24
  val conclude: thm -> thm
wenzelm@49845
    25
  val check_finished: Proof.context -> thm -> thm
wenzelm@32197
    26
  val finish: Proof.context -> thm -> thm
wenzelm@21604
    27
  val norm_result: thm -> thm
wenzelm@51222
    28
  val fork_name: string -> int -> (unit -> 'a) -> 'a future
wenzelm@51222
    29
  val fork: int -> (unit -> 'a) -> 'a future
wenzelm@49061
    30
  val peek_futures: serial -> unit future list
wenzelm@49931
    31
  val reset_futures: unit -> Future.group list
wenzelm@51276
    32
  val shutdown_futures: unit -> unit
wenzelm@49012
    33
  val future_enabled_level: int -> bool
wenzelm@29448
    34
  val future_enabled: unit -> bool
wenzelm@49012
    35
  val future_enabled_nested: int -> bool
wenzelm@51423
    36
  val future_enabled_timing: Time.time -> bool
wenzelm@28973
    37
  val future_result: Proof.context -> thm future -> term -> thm
wenzelm@23356
    38
  val prove_internal: cterm list -> cterm -> (thm list -> tactic) -> thm
wenzelm@20290
    39
  val prove_multi: Proof.context -> string list -> term list -> term list ->
wenzelm@20290
    40
    ({prems: thm list, context: Proof.context} -> tactic) -> thm list
wenzelm@28446
    41
  val prove_future: Proof.context -> string list -> term list -> term ->
wenzelm@28340
    42
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@20290
    43
  val prove: Proof.context -> string list -> term list -> term ->
wenzelm@20290
    44
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@51118
    45
  val prove_global_future: theory -> string list -> term list -> term ->
wenzelm@51118
    46
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@26713
    47
  val prove_global: theory -> string list -> term list -> term ->
wenzelm@26713
    48
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@51551
    49
  val prove_sorry: Proof.context -> string list -> term list -> term ->
wenzelm@51551
    50
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@51551
    51
  val prove_sorry_global: theory -> string list -> term list -> term ->
wenzelm@51551
    52
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@19184
    53
  val extract: int -> int -> thm -> thm Seq.seq
wenzelm@19184
    54
  val retrofit: int -> int -> thm -> thm -> thm Seq.seq
wenzelm@23418
    55
  val conjunction_tac: int -> tactic
wenzelm@23414
    56
  val precise_conjunction_tac: int -> int -> tactic
wenzelm@23418
    57
  val recover_conjunction_tac: tactic
wenzelm@21687
    58
  val norm_hhf_tac: int -> tactic
wenzelm@21687
    59
  val compose_hhf_tac: thm -> int -> tactic
wenzelm@23237
    60
  val assume_rule_tac: Proof.context -> int -> tactic
wenzelm@17980
    61
end;
wenzelm@17980
    62
wenzelm@17980
    63
structure Goal: GOAL =
wenzelm@17980
    64
struct
wenzelm@17980
    65
wenzelm@18027
    66
(** goals **)
wenzelm@18027
    67
wenzelm@18027
    68
(*
wenzelm@18027
    69
  -------- (init)
wenzelm@18027
    70
  C ==> #C
wenzelm@18027
    71
*)
wenzelm@20290
    72
val init =
wenzelm@22902
    73
  let val A = #1 (Thm.dest_implies (Thm.cprop_of Drule.protectI))
wenzelm@20290
    74
  in fn C => Thm.instantiate ([], [(A, C)]) Drule.protectI end;
wenzelm@17980
    75
wenzelm@17980
    76
(*
wenzelm@18119
    77
   C
wenzelm@18119
    78
  --- (protect)
wenzelm@18119
    79
  #C
wenzelm@17980
    80
*)
wenzelm@29345
    81
fun protect th = Drule.comp_no_flatten (th, 0) 1 Drule.protectI;
wenzelm@17980
    82
wenzelm@17980
    83
(*
wenzelm@18027
    84
  A ==> ... ==> #C
wenzelm@18027
    85
  ---------------- (conclude)
wenzelm@17980
    86
  A ==> ... ==> C
wenzelm@17980
    87
*)
wenzelm@29345
    88
fun conclude th = Drule.comp_no_flatten (th, Thm.nprems_of th) 1 Drule.protectD;
wenzelm@17980
    89
wenzelm@17980
    90
(*
wenzelm@18027
    91
  #C
wenzelm@18027
    92
  --- (finish)
wenzelm@18027
    93
   C
wenzelm@17983
    94
*)
wenzelm@32197
    95
fun check_finished ctxt th =
wenzelm@17980
    96
  (case Thm.nprems_of th of
wenzelm@49845
    97
    0 => th
wenzelm@17980
    98
  | n => raise THM ("Proof failed.\n" ^
wenzelm@49847
    99
      Pretty.string_of (Goal_Display.pretty_goal {main = true, limit = false} ctxt th), 0, [th]));
wenzelm@32197
   100
wenzelm@49845
   101
fun finish ctxt = check_finished ctxt #> conclude;
wenzelm@17980
   102
wenzelm@17980
   103
wenzelm@18027
   104
wenzelm@18027
   105
(** results **)
wenzelm@18027
   106
wenzelm@28340
   107
(* normal form *)
wenzelm@28340
   108
wenzelm@21604
   109
val norm_result =
wenzelm@21604
   110
  Drule.flexflex_unique
wenzelm@41228
   111
  #> Raw_Simplifier.norm_hhf_protect
wenzelm@21604
   112
  #> Thm.strip_shyps
wenzelm@21604
   113
  #> Drule.zero_var_indexes;
wenzelm@21604
   114
wenzelm@21604
   115
wenzelm@41703
   116
(* forked proofs *)
wenzelm@41703
   117
wenzelm@49061
   118
local
wenzelm@41703
   119
wenzelm@49061
   120
val forked_proofs =
wenzelm@49061
   121
  Synchronized.var "forked_proofs"
wenzelm@49061
   122
    (0, []: Future.group list, Inttab.empty: unit future list Inttab.table);
wenzelm@49061
   123
wenzelm@49061
   124
fun count_forked i =
wenzelm@49061
   125
  Synchronized.change forked_proofs (fn (m, groups, tab) =>
wenzelm@41703
   126
    let
wenzelm@41703
   127
      val n = m + i;
wenzelm@50974
   128
      val _ = Future.forked_proofs := n;
wenzelm@49061
   129
    in (n, groups, tab) end);
wenzelm@49061
   130
wenzelm@49061
   131
fun register_forked id future =
wenzelm@49061
   132
  Synchronized.change forked_proofs (fn (m, groups, tab) =>
wenzelm@49061
   133
    let
wenzelm@49061
   134
      val groups' = Task_Queue.group_of_task (Future.task_of future) :: groups;
wenzelm@49061
   135
      val tab' = Inttab.cons_list (id, Future.map (K ()) future) tab;
wenzelm@49061
   136
    in (m, groups', tab') end);
wenzelm@37186
   137
wenzelm@49036
   138
fun status task markups =
wenzelm@50201
   139
  let val props = Markup.properties [(Markup.taskN, Task_Queue.str_of_task task)]
wenzelm@49036
   140
  in Output.status (implode (map (Markup.markup_only o props) markups)) end;
wenzelm@49009
   141
wenzelm@49061
   142
in
wenzelm@49061
   143
wenzelm@51222
   144
fun fork_name name pri e =
wenzelm@41703
   145
  uninterruptible (fn _ => fn () =>
wenzelm@41703
   146
    let
wenzelm@50914
   147
      val pos = Position.thread_data ();
wenzelm@50914
   148
      val id = the_default 0 (Position.parse_id pos);
wenzelm@49061
   149
      val _ = count_forked 1;
wenzelm@50914
   150
wenzelm@41703
   151
      val future =
wenzelm@44302
   152
        (singleton o Future.forks)
wenzelm@51222
   153
          {name = name, group = NONE, deps = [], pri = pri, interrupts = false}
wenzelm@49036
   154
          (fn () =>
wenzelm@49036
   155
            let
wenzelm@49036
   156
              val task = the (Future.worker_task ());
wenzelm@50201
   157
              val _ = status task [Markup.running];
wenzelm@50914
   158
              val result =
wenzelm@50914
   159
                Exn.capture (Future.interruptible_task e) ()
wenzelm@50914
   160
                |> Future.identify_result pos;
wenzelm@50201
   161
              val _ = status task [Markup.finished, Markup.joined];
wenzelm@49036
   162
              val _ =
wenzelm@49041
   163
                (case result of
wenzelm@49041
   164
                  Exn.Res _ => ()
wenzelm@49041
   165
                | Exn.Exn exn =>
wenzelm@49830
   166
                    if id = 0 orelse Exn.is_interrupt exn then ()
wenzelm@49829
   167
                    else
wenzelm@50201
   168
                      (status task [Markup.failed];
wenzelm@50201
   169
                       Output.report (Markup.markup_only Markup.bad);
wenzelm@50914
   170
                       List.app (Future.error_msg pos) (ML_Compiler.exn_messages_ids exn)));
wenzelm@49061
   171
              val _ = count_forked ~1;
wenzelm@49036
   172
            in Exn.release result end);
wenzelm@50201
   173
      val _ = status (Future.task_of future) [Markup.forked];
wenzelm@49061
   174
      val _ = register_forked id future;
wenzelm@41703
   175
    in future end) ();
wenzelm@29448
   176
wenzelm@51222
   177
fun fork pri e = fork_name "Goal.fork" pri e;
wenzelm@41677
   178
wenzelm@49061
   179
fun forked_count () = #1 (Synchronized.value forked_proofs);
wenzelm@49061
   180
wenzelm@49061
   181
fun peek_futures id =
wenzelm@49061
   182
  Inttab.lookup_list (#3 (Synchronized.value forked_proofs)) id;
wenzelm@49061
   183
wenzelm@49931
   184
fun reset_futures () =
wenzelm@49931
   185
  Synchronized.change_result forked_proofs (fn (m, groups, tab) =>
wenzelm@50974
   186
    (Future.forked_proofs := 0; (groups, (0, [], Inttab.empty))));
wenzelm@49061
   187
wenzelm@51276
   188
fun shutdown_futures () =
wenzelm@51276
   189
  (Future.shutdown ();
wenzelm@51276
   190
    (case map_filter Task_Queue.group_status (reset_futures ()) of
wenzelm@51276
   191
      [] => ()
wenzelm@51276
   192
    | exns => raise Par_Exn.make exns));
wenzelm@51276
   193
wenzelm@49061
   194
end;
wenzelm@49061
   195
wenzelm@41677
   196
wenzelm@41703
   197
(* scheduling parameters *)
wenzelm@41703
   198
wenzelm@32738
   199
val parallel_proofs = Unsynchronized.ref 1;
wenzelm@51423
   200
val parallel_subproofs_saturation = Unsynchronized.ref 100;
wenzelm@51423
   201
val parallel_subproofs_threshold = Unsynchronized.ref 0.01;
wenzelm@29448
   202
wenzelm@49012
   203
fun future_enabled_level n =
wenzelm@49012
   204
  Multithreading.enabled () andalso ! parallel_proofs >= n andalso
wenzelm@41703
   205
  is_some (Future.worker_task ());
wenzelm@32061
   206
wenzelm@49012
   207
fun future_enabled () = future_enabled_level 1;
wenzelm@49012
   208
wenzelm@49012
   209
fun future_enabled_nested n =
wenzelm@49012
   210
  future_enabled_level n andalso
wenzelm@51423
   211
  forked_count () < ! parallel_subproofs_saturation * Multithreading.max_threads_value ();
wenzelm@51423
   212
wenzelm@51423
   213
fun future_enabled_timing t =
wenzelm@51423
   214
  future_enabled () andalso Time.toReal t >= ! parallel_subproofs_threshold;
wenzelm@29448
   215
wenzelm@29448
   216
wenzelm@28446
   217
(* future_result *)
wenzelm@28340
   218
wenzelm@28446
   219
fun future_result ctxt result prop =
wenzelm@28340
   220
  let
wenzelm@42360
   221
    val thy = Proof_Context.theory_of ctxt;
wenzelm@28340
   222
    val _ = Context.reject_draft thy;
wenzelm@28340
   223
    val cert = Thm.cterm_of thy;
wenzelm@28340
   224
    val certT = Thm.ctyp_of thy;
wenzelm@28340
   225
wenzelm@30473
   226
    val assms = Assumption.all_assms_of ctxt;
wenzelm@28340
   227
    val As = map Thm.term_of assms;
wenzelm@28340
   228
wenzelm@28340
   229
    val xs = map Free (fold Term.add_frees (prop :: As) []);
wenzelm@28340
   230
    val fixes = map cert xs;
wenzelm@28340
   231
wenzelm@28340
   232
    val tfrees = fold Term.add_tfrees (prop :: As) [];
wenzelm@28340
   233
    val instT = map (fn (a, S) => (certT (TVar ((a, 0), S)), certT (TFree (a, S)))) tfrees;
wenzelm@28340
   234
wenzelm@28340
   235
    val global_prop =
wenzelm@45344
   236
      cert (Logic.varify_types_global (fold_rev Logic.all xs (Logic.list_implies (As, prop))))
wenzelm@32056
   237
      |> Thm.weaken_sorts (Variable.sorts_of ctxt);
wenzelm@28973
   238
    val global_result = result |> Future.map
wenzelm@33768
   239
      (Drule.flexflex_unique #>
wenzelm@33768
   240
        Thm.adjust_maxidx_thm ~1 #>
wenzelm@28973
   241
        Drule.implies_intr_list assms #>
wenzelm@28973
   242
        Drule.forall_intr_list fixes #>
wenzelm@36613
   243
        Thm.generalize (map #1 tfrees, []) 0 #>
wenzelm@36613
   244
        Thm.strip_shyps);
wenzelm@28340
   245
    val local_result =
wenzelm@32056
   246
      Thm.future global_result global_prop
wenzelm@50987
   247
      |> Thm.close_derivation
wenzelm@28340
   248
      |> Thm.instantiate (instT, [])
wenzelm@28340
   249
      |> Drule.forall_elim_list fixes
wenzelm@28340
   250
      |> fold (Thm.elim_implies o Thm.assume) assms;
wenzelm@28340
   251
  in local_result end;
wenzelm@28340
   252
wenzelm@28340
   253
wenzelm@18027
   254
wenzelm@18027
   255
(** tactical theorem proving **)
wenzelm@18027
   256
wenzelm@23356
   257
(* prove_internal -- minimal checks, no normalization of result! *)
wenzelm@20250
   258
wenzelm@23356
   259
fun prove_internal casms cprop tac =
wenzelm@20250
   260
  (case SINGLE (tac (map Assumption.assume casms)) (init cprop) of
wenzelm@32197
   261
    SOME th => Drule.implies_intr_list casms
wenzelm@32197
   262
      (finish (Syntax.init_pretty_global (Thm.theory_of_thm th)) th)
wenzelm@38875
   263
  | NONE => error "Tactic failed");
wenzelm@20250
   264
wenzelm@20250
   265
wenzelm@28340
   266
(* prove_common etc. *)
wenzelm@17986
   267
wenzelm@28340
   268
fun prove_common immediate ctxt xs asms props tac =
wenzelm@17980
   269
  let
wenzelm@42360
   270
    val thy = Proof_Context.theory_of ctxt;
wenzelm@26939
   271
    val string_of_term = Syntax.string_of_term ctxt;
wenzelm@20056
   272
wenzelm@28355
   273
    val pos = Position.thread_data ();
wenzelm@20250
   274
    fun err msg = cat_error msg
wenzelm@20250
   275
      ("The error(s) above occurred for the goal statement:\n" ^
wenzelm@28355
   276
        string_of_term (Logic.list_implies (asms, Logic.mk_conjunction_list props)) ^
wenzelm@48992
   277
        (case Position.here pos of "" => "" | s => "\n" ^ s));
wenzelm@17980
   278
wenzelm@20250
   279
    fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm (Term.no_dummy_patterns t))
wenzelm@17980
   280
      handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
wenzelm@20250
   281
    val casms = map cert_safe asms;
wenzelm@20250
   282
    val cprops = map cert_safe props;
wenzelm@17980
   283
wenzelm@20250
   284
    val (prems, ctxt') = ctxt
wenzelm@20250
   285
      |> Variable.add_fixes_direct xs
wenzelm@27218
   286
      |> fold Variable.declare_term (asms @ props)
wenzelm@26713
   287
      |> Assumption.add_assumes casms
wenzelm@26713
   288
      ||> Variable.set_body true;
wenzelm@28619
   289
    val sorts = Variable.sorts_of ctxt';
wenzelm@17980
   290
wenzelm@28619
   291
    val stmt = Thm.weaken_sorts sorts (Conjunction.mk_conjunction_balanced cprops);
wenzelm@28340
   292
wenzelm@28340
   293
    fun result () =
wenzelm@28340
   294
      (case SINGLE (tac {prems = prems, context = ctxt'}) (init stmt) of
wenzelm@38875
   295
        NONE => err "Tactic failed"
wenzelm@28340
   296
      | SOME st =>
wenzelm@32197
   297
          let val res = finish ctxt' st handle THM (msg, _, _) => err msg in
wenzelm@28619
   298
            if Unify.matches_list thy [Thm.term_of stmt] [Thm.prop_of res]
wenzelm@28619
   299
            then Thm.check_shyps sorts res
wenzelm@28340
   300
            else err ("Proved a different theorem: " ^ string_of_term (Thm.prop_of res))
wenzelm@28340
   301
          end);
wenzelm@28340
   302
    val res =
wenzelm@29448
   303
      if immediate orelse #maxidx (Thm.rep_cterm stmt) >= 0 orelse not (future_enabled ())
wenzelm@29088
   304
      then result ()
wenzelm@51222
   305
      else future_result ctxt' (fork ~1 result) (Thm.term_of stmt);
wenzelm@17980
   306
  in
wenzelm@28340
   307
    Conjunction.elim_balanced (length props) res
wenzelm@20290
   308
    |> map (Assumption.export false ctxt' ctxt)
wenzelm@20056
   309
    |> Variable.export ctxt' ctxt
wenzelm@20250
   310
    |> map Drule.zero_var_indexes
wenzelm@17980
   311
  end;
wenzelm@17980
   312
wenzelm@28341
   313
val prove_multi = prove_common true;
wenzelm@17980
   314
wenzelm@28446
   315
fun prove_future ctxt xs asms prop tac = hd (prove_common false ctxt xs asms [prop] tac);
wenzelm@51118
   316
wenzelm@28340
   317
fun prove ctxt xs asms prop tac = hd (prove_common true ctxt xs asms [prop] tac);
wenzelm@20056
   318
wenzelm@51118
   319
fun prove_global_future thy xs asms prop tac =
wenzelm@51118
   320
  Drule.export_without_context (prove_future (Proof_Context.init_global thy) xs asms prop tac);
wenzelm@51118
   321
wenzelm@20056
   322
fun prove_global thy xs asms prop tac =
wenzelm@42360
   323
  Drule.export_without_context (prove (Proof_Context.init_global thy) xs asms prop tac);
wenzelm@18027
   324
wenzelm@51551
   325
fun prove_sorry ctxt xs asms prop tac =
wenzelm@51551
   326
  if ! quick_and_dirty then
wenzelm@51552
   327
    prove ctxt xs asms prop (fn _ => ALLGOALS Skip_Proof.cheat_tac)
wenzelm@51551
   328
  else (if future_enabled () then prove_future else prove) ctxt xs asms prop tac;
wenzelm@51551
   329
wenzelm@51551
   330
fun prove_sorry_global thy xs asms prop tac =
wenzelm@51551
   331
  Drule.export_without_context
wenzelm@51551
   332
    (prove_sorry (Proof_Context.init_global thy) xs asms prop tac);
wenzelm@51551
   333
wenzelm@18027
   334
wenzelm@17980
   335
wenzelm@21687
   336
(** goal structure **)
wenzelm@21687
   337
wenzelm@21687
   338
(* nested goals *)
wenzelm@18207
   339
wenzelm@19184
   340
fun extract i n st =
wenzelm@19184
   341
  (if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st then Seq.empty
wenzelm@19184
   342
   else if n = 1 then Seq.single (Thm.cprem_of st i)
wenzelm@23418
   343
   else
wenzelm@23418
   344
     Seq.single (Conjunction.mk_conjunction_balanced (map (Thm.cprem_of st) (i upto i + n - 1))))
wenzelm@20260
   345
  |> Seq.map (Thm.adjust_maxidx_cterm ~1 #> init);
wenzelm@17980
   346
wenzelm@19221
   347
fun retrofit i n st' st =
wenzelm@19221
   348
  (if n = 1 then st
wenzelm@23418
   349
   else st |> Drule.with_subgoal i (Conjunction.uncurry_balanced n))
wenzelm@19221
   350
  |> Thm.compose_no_flatten false (conclude st', Thm.nprems_of st') i;
wenzelm@18207
   351
wenzelm@17980
   352
fun SELECT_GOAL tac i st =
wenzelm@19191
   353
  if Thm.nprems_of st = 1 andalso i = 1 then tac st
wenzelm@19184
   354
  else Seq.lifts (retrofit i 1) (Seq.maps tac (extract i 1 st)) st;
wenzelm@17980
   355
wenzelm@21687
   356
wenzelm@21687
   357
(* multiple goals *)
wenzelm@21687
   358
wenzelm@23418
   359
fun precise_conjunction_tac 0 i = eq_assume_tac i
wenzelm@23418
   360
  | precise_conjunction_tac 1 i = SUBGOAL (K all_tac) i
wenzelm@23418
   361
  | precise_conjunction_tac n i = PRIMITIVE (Drule.with_subgoal i (Conjunction.curry_balanced n));
wenzelm@23414
   362
wenzelm@23418
   363
val adhoc_conjunction_tac = REPEAT_ALL_NEW
wenzelm@23418
   364
  (SUBGOAL (fn (goal, i) =>
wenzelm@23418
   365
    if can Logic.dest_conjunction goal then rtac Conjunction.conjunctionI i
wenzelm@23418
   366
    else no_tac));
wenzelm@21687
   367
wenzelm@23418
   368
val conjunction_tac = SUBGOAL (fn (goal, i) =>
wenzelm@23418
   369
  precise_conjunction_tac (length (Logic.dest_conjunctions goal)) i ORELSE
wenzelm@23418
   370
  TRY (adhoc_conjunction_tac i));
wenzelm@21687
   371
wenzelm@23418
   372
val recover_conjunction_tac = PRIMITIVE (fn th =>
wenzelm@23418
   373
  Conjunction.uncurry_balanced (Thm.nprems_of th) th);
wenzelm@23414
   374
wenzelm@23414
   375
fun PRECISE_CONJUNCTS n tac =
wenzelm@23414
   376
  SELECT_GOAL (precise_conjunction_tac n 1
wenzelm@23414
   377
    THEN tac
wenzelm@23418
   378
    THEN recover_conjunction_tac);
wenzelm@23414
   379
wenzelm@21687
   380
fun CONJUNCTS tac =
wenzelm@21687
   381
  SELECT_GOAL (conjunction_tac 1
wenzelm@21687
   382
    THEN tac
wenzelm@23418
   383
    THEN recover_conjunction_tac);
wenzelm@21687
   384
wenzelm@21687
   385
wenzelm@21687
   386
(* hhf normal form *)
wenzelm@21687
   387
wenzelm@21687
   388
val norm_hhf_tac =
wenzelm@21687
   389
  rtac Drule.asm_rl  (*cheap approximation -- thanks to builtin Logic.flatten_params*)
wenzelm@21687
   390
  THEN' SUBGOAL (fn (t, i) =>
wenzelm@21687
   391
    if Drule.is_norm_hhf t then all_tac
wenzelm@41228
   392
    else rewrite_goal_tac Drule.norm_hhf_eqs i);
wenzelm@21687
   393
wenzelm@25301
   394
fun compose_hhf_tac th i st =
wenzelm@25301
   395
  PRIMSEQ (Thm.bicompose false (false, Drule.lift_all (Thm.cprem_of st i) th, 0) i) st;
wenzelm@21687
   396
wenzelm@23237
   397
wenzelm@23237
   398
(* non-atomic goal assumptions *)
wenzelm@23237
   399
wenzelm@23356
   400
fun non_atomic (Const ("==>", _) $ _ $ _) = true
wenzelm@23356
   401
  | non_atomic (Const ("all", _) $ _) = true
wenzelm@23356
   402
  | non_atomic _ = false;
wenzelm@23356
   403
wenzelm@23237
   404
fun assume_rule_tac ctxt = norm_hhf_tac THEN' CSUBGOAL (fn (goal, i) =>
wenzelm@23237
   405
  let
wenzelm@42495
   406
    val ((_, goal'), ctxt') = Variable.focus_cterm goal ctxt;
wenzelm@23237
   407
    val goal'' = Drule.cterm_rule (singleton (Variable.export ctxt' ctxt)) goal';
wenzelm@23356
   408
    val Rs = filter (non_atomic o Thm.term_of) (Drule.strip_imp_prems goal'');
wenzelm@23237
   409
    val tacs = Rs |> map (fn R =>
wenzelm@41228
   410
      Tactic.etac (Raw_Simplifier.norm_hhf (Thm.trivial R)) THEN_ALL_NEW assume_tac);
wenzelm@23237
   411
  in fold_rev (curry op APPEND') tacs (K no_tac) i end);
wenzelm@23237
   412
wenzelm@32365
   413
wenzelm@32365
   414
(* parallel tacticals *)
wenzelm@32365
   415
wenzelm@32365
   416
(*parallel choice of single results*)
wenzelm@32365
   417
fun PARALLEL_CHOICE tacs st =
wenzelm@32365
   418
  (case Par_List.get_some (fn tac => SINGLE tac st) tacs of
wenzelm@32365
   419
    NONE => Seq.empty
wenzelm@32365
   420
  | SOME st' => Seq.single st');
wenzelm@32365
   421
wenzelm@32365
   422
(*parallel refinement of non-schematic goal by single results*)
wenzelm@32788
   423
exception FAILED of unit;
wenzelm@42370
   424
fun PARALLEL_GOALS tac =
wenzelm@42370
   425
  Thm.adjust_maxidx_thm ~1 #>
wenzelm@42370
   426
  (fn st =>
wenzelm@42371
   427
    if not (Multithreading.enabled ()) orelse Thm.maxidx_of st >= 0 orelse Thm.nprems_of st <= 1
wenzelm@42370
   428
    then DETERM tac st
wenzelm@42370
   429
    else
wenzelm@42370
   430
      let
wenzelm@42370
   431
        fun try_tac g =
wenzelm@42370
   432
          (case SINGLE tac g of
wenzelm@42370
   433
            NONE => raise FAILED ()
wenzelm@42370
   434
          | SOME g' => g');
wenzelm@32365
   435
wenzelm@42370
   436
        val goals = Drule.strip_imp_prems (Thm.cprop_of st);
wenzelm@42370
   437
        val results = Par_List.map (try_tac o init) goals;
wenzelm@42370
   438
      in ALLGOALS (fn i => retrofit i 1 (nth results (i - 1))) st end
wenzelm@42370
   439
      handle FAILED () => Seq.empty);
wenzelm@32365
   440
wenzelm@18207
   441
end;
wenzelm@18207
   442
wenzelm@32365
   443
structure Basic_Goal: BASIC_GOAL = Goal;
wenzelm@32365
   444
open Basic_Goal;