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