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