src/Pure/goal.ML
author wenzelm
Wed Jun 26 11:54:45 2013 +0200 (2013-06-26)
changeset 52456 960202346d0c
parent 52223 5bb6ae8acb87
child 52458 210bca64b894
permissions -rw-r--r--
tuned signature;
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@51553
     9
  val skip_proofs: bool Unsynchronized.ref
wenzelm@32738
    10
  val parallel_proofs: 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@52456
    22
  val protect: int -> 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@51605
    27
  val fork_params: {name: string, pos: Position.T, pri: 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@51423
    35
  val future_enabled_timing: Time.time -> bool
wenzelm@28973
    36
  val future_result: Proof.context -> thm future -> term -> thm
wenzelm@23356
    37
  val prove_internal: cterm list -> cterm -> (thm list -> tactic) -> thm
wenzelm@51553
    38
  val is_schematic: term -> bool
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@23237
    59
  val assume_rule_tac: Proof.context -> int -> tactic
wenzelm@17980
    60
end;
wenzelm@17980
    61
wenzelm@17980
    62
structure Goal: GOAL =
wenzelm@17980
    63
struct
wenzelm@17980
    64
wenzelm@18027
    65
(** goals **)
wenzelm@18027
    66
wenzelm@18027
    67
(*
wenzelm@18027
    68
  -------- (init)
wenzelm@18027
    69
  C ==> #C
wenzelm@18027
    70
*)
wenzelm@20290
    71
val init =
wenzelm@22902
    72
  let val A = #1 (Thm.dest_implies (Thm.cprop_of Drule.protectI))
wenzelm@20290
    73
  in fn C => Thm.instantiate ([], [(A, C)]) Drule.protectI end;
wenzelm@17980
    74
wenzelm@17980
    75
(*
wenzelm@52456
    76
  A1 ==> ... ==> An ==> C
wenzelm@52456
    77
  ------------------------ (protect n)
wenzelm@52456
    78
  A1 ==> ... ==> An ==> #C
wenzelm@17980
    79
*)
wenzelm@52456
    80
fun protect n th = Drule.comp_no_flatten (th, n) 1 Drule.protectI;
wenzelm@17980
    81
wenzelm@17980
    82
(*
wenzelm@18027
    83
  A ==> ... ==> #C
wenzelm@18027
    84
  ---------------- (conclude)
wenzelm@17980
    85
  A ==> ... ==> C
wenzelm@17980
    86
*)
wenzelm@29345
    87
fun conclude th = Drule.comp_no_flatten (th, Thm.nprems_of th) 1 Drule.protectD;
wenzelm@17980
    88
wenzelm@17980
    89
(*
wenzelm@18027
    90
  #C
wenzelm@18027
    91
  --- (finish)
wenzelm@18027
    92
   C
wenzelm@17983
    93
*)
wenzelm@32197
    94
fun check_finished ctxt th =
wenzelm@51608
    95
  if Thm.no_prems th then th
wenzelm@51608
    96
  else
wenzelm@51958
    97
    raise THM ("Proof failed.\n" ^ Pretty.string_of (Goal_Display.pretty_goal ctxt th), 0, [th]);
wenzelm@32197
    98
wenzelm@49845
    99
fun finish ctxt = check_finished ctxt #> conclude;
wenzelm@17980
   100
wenzelm@17980
   101
wenzelm@18027
   102
wenzelm@18027
   103
(** results **)
wenzelm@18027
   104
wenzelm@28340
   105
(* normal form *)
wenzelm@28340
   106
wenzelm@21604
   107
val norm_result =
wenzelm@21604
   108
  Drule.flexflex_unique
wenzelm@41228
   109
  #> Raw_Simplifier.norm_hhf_protect
wenzelm@21604
   110
  #> Thm.strip_shyps
wenzelm@21604
   111
  #> Drule.zero_var_indexes;
wenzelm@21604
   112
wenzelm@21604
   113
wenzelm@41703
   114
(* forked proofs *)
wenzelm@41703
   115
wenzelm@49061
   116
local
wenzelm@41703
   117
wenzelm@49061
   118
val forked_proofs =
wenzelm@49061
   119
  Synchronized.var "forked_proofs"
wenzelm@49061
   120
    (0, []: Future.group list, Inttab.empty: unit future list Inttab.table);
wenzelm@49061
   121
wenzelm@49061
   122
fun count_forked i =
wenzelm@49061
   123
  Synchronized.change forked_proofs (fn (m, groups, tab) =>
wenzelm@41703
   124
    let
wenzelm@41703
   125
      val n = m + i;
wenzelm@50974
   126
      val _ = Future.forked_proofs := n;
wenzelm@49061
   127
    in (n, groups, tab) end);
wenzelm@49061
   128
wenzelm@49061
   129
fun register_forked id future =
wenzelm@49061
   130
  Synchronized.change forked_proofs (fn (m, groups, tab) =>
wenzelm@49061
   131
    let
wenzelm@49061
   132
      val groups' = Task_Queue.group_of_task (Future.task_of future) :: groups;
wenzelm@49061
   133
      val tab' = Inttab.cons_list (id, Future.map (K ()) future) tab;
wenzelm@49061
   134
    in (m, groups', tab') end);
wenzelm@37186
   135
wenzelm@49036
   136
fun status task markups =
wenzelm@50201
   137
  let val props = Markup.properties [(Markup.taskN, Task_Queue.str_of_task task)]
wenzelm@49036
   138
  in Output.status (implode (map (Markup.markup_only o props) markups)) end;
wenzelm@49009
   139
wenzelm@49061
   140
in
wenzelm@49061
   141
wenzelm@51605
   142
fun fork_params {name, pos, pri} e =
wenzelm@51607
   143
  uninterruptible (fn _ => Position.setmp_thread_data pos (fn () =>
wenzelm@41703
   144
    let
wenzelm@50914
   145
      val id = the_default 0 (Position.parse_id pos);
wenzelm@49061
   146
      val _ = count_forked 1;
wenzelm@50914
   147
wenzelm@41703
   148
      val future =
wenzelm@44302
   149
        (singleton o Future.forks)
wenzelm@51222
   150
          {name = name, group = NONE, deps = [], pri = pri, interrupts = false}
wenzelm@49036
   151
          (fn () =>
wenzelm@49036
   152
            let
wenzelm@49036
   153
              val task = the (Future.worker_task ());
wenzelm@50201
   154
              val _ = status task [Markup.running];
wenzelm@50914
   155
              val result =
wenzelm@50914
   156
                Exn.capture (Future.interruptible_task e) ()
wenzelm@50914
   157
                |> Future.identify_result pos;
wenzelm@50201
   158
              val _ = status task [Markup.finished, Markup.joined];
wenzelm@49036
   159
              val _ =
wenzelm@49041
   160
                (case result of
wenzelm@49041
   161
                  Exn.Res _ => ()
wenzelm@49041
   162
                | Exn.Exn exn =>
wenzelm@49830
   163
                    if id = 0 orelse Exn.is_interrupt exn then ()
wenzelm@49829
   164
                    else
wenzelm@50201
   165
                      (status task [Markup.failed];
wenzelm@50201
   166
                       Output.report (Markup.markup_only Markup.bad);
wenzelm@50914
   167
                       List.app (Future.error_msg pos) (ML_Compiler.exn_messages_ids exn)));
wenzelm@49061
   168
              val _ = count_forked ~1;
wenzelm@49036
   169
            in Exn.release result end);
wenzelm@50201
   170
      val _ = status (Future.task_of future) [Markup.forked];
wenzelm@49061
   171
      val _ = register_forked id future;
wenzelm@51607
   172
    in future end)) ();
wenzelm@29448
   173
wenzelm@51605
   174
fun fork pri e =
wenzelm@51605
   175
  fork_params {name = "Goal.fork", pos = Position.thread_data (), pri = pri} e;
wenzelm@41677
   176
wenzelm@49061
   177
fun forked_count () = #1 (Synchronized.value forked_proofs);
wenzelm@49061
   178
wenzelm@49061
   179
fun peek_futures id =
wenzelm@49061
   180
  Inttab.lookup_list (#3 (Synchronized.value forked_proofs)) id;
wenzelm@49061
   181
wenzelm@49931
   182
fun reset_futures () =
wenzelm@51608
   183
  Synchronized.change_result forked_proofs (fn (_, groups, _) =>
wenzelm@50974
   184
    (Future.forked_proofs := 0; (groups, (0, [], Inttab.empty))));
wenzelm@49061
   185
wenzelm@51276
   186
fun shutdown_futures () =
wenzelm@51276
   187
  (Future.shutdown ();
wenzelm@51276
   188
    (case map_filter Task_Queue.group_status (reset_futures ()) of
wenzelm@51276
   189
      [] => ()
wenzelm@51276
   190
    | exns => raise Par_Exn.make exns));
wenzelm@51276
   191
wenzelm@49061
   192
end;
wenzelm@49061
   193
wenzelm@41677
   194
wenzelm@41703
   195
(* scheduling parameters *)
wenzelm@41703
   196
wenzelm@51553
   197
val skip_proofs = Unsynchronized.ref false;
wenzelm@51553
   198
wenzelm@32738
   199
val parallel_proofs = Unsynchronized.ref 1;
wenzelm@29448
   200
wenzelm@49012
   201
fun future_enabled_level n =
wenzelm@49012
   202
  Multithreading.enabled () andalso ! parallel_proofs >= n andalso
wenzelm@41703
   203
  is_some (Future.worker_task ());
wenzelm@32061
   204
wenzelm@49012
   205
fun future_enabled () = future_enabled_level 1;
wenzelm@49012
   206
wenzelm@49012
   207
fun future_enabled_nested n =
wenzelm@49012
   208
  future_enabled_level n andalso
wenzelm@52104
   209
  forked_count () <
wenzelm@52104
   210
    Options.default_int "parallel_subproofs_saturation" * Multithreading.max_threads_value ();
wenzelm@51423
   211
wenzelm@51423
   212
fun future_enabled_timing t =
wenzelm@52104
   213
  future_enabled () andalso
wenzelm@52104
   214
    Time.toReal t >= Options.default_real "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@51553
   266
(* prove variations *)
wenzelm@51553
   267
wenzelm@51553
   268
fun is_schematic t =
wenzelm@51553
   269
  Term.exists_subterm Term.is_Var t orelse
wenzelm@51553
   270
  Term.exists_type (Term.exists_subtype Term.is_TVar) t;
wenzelm@17986
   271
wenzelm@28340
   272
fun prove_common immediate ctxt xs asms props tac =
wenzelm@17980
   273
  let
wenzelm@42360
   274
    val thy = Proof_Context.theory_of ctxt;
wenzelm@26939
   275
    val string_of_term = Syntax.string_of_term ctxt;
wenzelm@20056
   276
wenzelm@51553
   277
    val schematic = exists is_schematic props;
wenzelm@51553
   278
    val future = future_enabled ();
wenzelm@51553
   279
    val skip = not immediate andalso not schematic andalso future andalso ! skip_proofs;
wenzelm@51553
   280
wenzelm@28355
   281
    val pos = Position.thread_data ();
wenzelm@20250
   282
    fun err msg = cat_error msg
wenzelm@20250
   283
      ("The error(s) above occurred for the goal statement:\n" ^
wenzelm@28355
   284
        string_of_term (Logic.list_implies (asms, Logic.mk_conjunction_list props)) ^
wenzelm@48992
   285
        (case Position.here pos of "" => "" | s => "\n" ^ s));
wenzelm@17980
   286
wenzelm@20250
   287
    fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm (Term.no_dummy_patterns t))
wenzelm@17980
   288
      handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
wenzelm@20250
   289
    val casms = map cert_safe asms;
wenzelm@20250
   290
    val cprops = map cert_safe props;
wenzelm@17980
   291
wenzelm@20250
   292
    val (prems, ctxt') = ctxt
wenzelm@20250
   293
      |> Variable.add_fixes_direct xs
wenzelm@27218
   294
      |> fold Variable.declare_term (asms @ props)
wenzelm@26713
   295
      |> Assumption.add_assumes casms
wenzelm@26713
   296
      ||> Variable.set_body true;
wenzelm@28619
   297
    val sorts = Variable.sorts_of ctxt';
wenzelm@17980
   298
wenzelm@28619
   299
    val stmt = Thm.weaken_sorts sorts (Conjunction.mk_conjunction_balanced cprops);
wenzelm@28340
   300
wenzelm@51553
   301
    fun tac' args st =
wenzelm@51553
   302
      if skip then ALLGOALS Skip_Proof.cheat_tac st before Skip_Proof.report ctxt
wenzelm@51553
   303
      else tac args st;
wenzelm@28340
   304
    fun result () =
wenzelm@51553
   305
      (case SINGLE (tac' {prems = prems, context = ctxt'}) (init stmt) of
wenzelm@38875
   306
        NONE => err "Tactic failed"
wenzelm@28340
   307
      | SOME st =>
wenzelm@32197
   308
          let val res = finish ctxt' st handle THM (msg, _, _) => err msg in
wenzelm@28619
   309
            if Unify.matches_list thy [Thm.term_of stmt] [Thm.prop_of res]
wenzelm@28619
   310
            then Thm.check_shyps sorts res
wenzelm@28340
   311
            else err ("Proved a different theorem: " ^ string_of_term (Thm.prop_of res))
wenzelm@28340
   312
          end);
wenzelm@28340
   313
    val res =
wenzelm@51553
   314
      if immediate orelse schematic orelse not future orelse skip
wenzelm@29088
   315
      then result ()
wenzelm@51222
   316
      else future_result ctxt' (fork ~1 result) (Thm.term_of stmt);
wenzelm@17980
   317
  in
wenzelm@28340
   318
    Conjunction.elim_balanced (length props) res
wenzelm@20290
   319
    |> map (Assumption.export false ctxt' ctxt)
wenzelm@20056
   320
    |> Variable.export ctxt' ctxt
wenzelm@20250
   321
    |> map Drule.zero_var_indexes
wenzelm@17980
   322
  end;
wenzelm@17980
   323
wenzelm@28341
   324
val prove_multi = prove_common true;
wenzelm@17980
   325
wenzelm@28446
   326
fun prove_future ctxt xs asms prop tac = hd (prove_common false ctxt xs asms [prop] tac);
wenzelm@51118
   327
wenzelm@28340
   328
fun prove ctxt xs asms prop tac = hd (prove_common true ctxt xs asms [prop] tac);
wenzelm@20056
   329
wenzelm@51118
   330
fun prove_global_future thy xs asms prop tac =
wenzelm@51118
   331
  Drule.export_without_context (prove_future (Proof_Context.init_global thy) xs asms prop tac);
wenzelm@51118
   332
wenzelm@20056
   333
fun prove_global thy xs asms prop tac =
wenzelm@42360
   334
  Drule.export_without_context (prove (Proof_Context.init_global thy) xs asms prop tac);
wenzelm@18027
   335
wenzelm@51551
   336
fun prove_sorry ctxt xs asms prop tac =
wenzelm@52059
   337
  if Config.get ctxt quick_and_dirty then
wenzelm@51552
   338
    prove ctxt xs asms prop (fn _ => ALLGOALS Skip_Proof.cheat_tac)
wenzelm@51551
   339
  else (if future_enabled () then prove_future else prove) ctxt xs asms prop tac;
wenzelm@51551
   340
wenzelm@51551
   341
fun prove_sorry_global thy xs asms prop tac =
wenzelm@51551
   342
  Drule.export_without_context
wenzelm@51551
   343
    (prove_sorry (Proof_Context.init_global thy) xs asms prop tac);
wenzelm@51551
   344
wenzelm@18027
   345
wenzelm@17980
   346
wenzelm@21687
   347
(** goal structure **)
wenzelm@21687
   348
wenzelm@21687
   349
(* nested goals *)
wenzelm@18207
   350
wenzelm@19184
   351
fun extract i n st =
wenzelm@19184
   352
  (if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st then Seq.empty
wenzelm@19184
   353
   else if n = 1 then Seq.single (Thm.cprem_of st i)
wenzelm@23418
   354
   else
wenzelm@23418
   355
     Seq.single (Conjunction.mk_conjunction_balanced (map (Thm.cprem_of st) (i upto i + n - 1))))
wenzelm@20260
   356
  |> Seq.map (Thm.adjust_maxidx_cterm ~1 #> init);
wenzelm@17980
   357
wenzelm@19221
   358
fun retrofit i n st' st =
wenzelm@19221
   359
  (if n = 1 then st
wenzelm@23418
   360
   else st |> Drule.with_subgoal i (Conjunction.uncurry_balanced n))
wenzelm@52223
   361
  |> Thm.bicompose {flatten = false, match = false, incremented = false}
wenzelm@52223
   362
      (false, conclude st', Thm.nprems_of st') i;
wenzelm@18207
   363
wenzelm@17980
   364
fun SELECT_GOAL tac i st =
wenzelm@19191
   365
  if Thm.nprems_of st = 1 andalso i = 1 then tac st
wenzelm@19184
   366
  else Seq.lifts (retrofit i 1) (Seq.maps tac (extract i 1 st)) st;
wenzelm@17980
   367
wenzelm@21687
   368
wenzelm@21687
   369
(* multiple goals *)
wenzelm@21687
   370
wenzelm@23418
   371
fun precise_conjunction_tac 0 i = eq_assume_tac i
wenzelm@23418
   372
  | precise_conjunction_tac 1 i = SUBGOAL (K all_tac) i
wenzelm@23418
   373
  | precise_conjunction_tac n i = PRIMITIVE (Drule.with_subgoal i (Conjunction.curry_balanced n));
wenzelm@23414
   374
wenzelm@23418
   375
val adhoc_conjunction_tac = REPEAT_ALL_NEW
wenzelm@23418
   376
  (SUBGOAL (fn (goal, i) =>
wenzelm@23418
   377
    if can Logic.dest_conjunction goal then rtac Conjunction.conjunctionI i
wenzelm@23418
   378
    else no_tac));
wenzelm@21687
   379
wenzelm@23418
   380
val conjunction_tac = SUBGOAL (fn (goal, i) =>
wenzelm@23418
   381
  precise_conjunction_tac (length (Logic.dest_conjunctions goal)) i ORELSE
wenzelm@23418
   382
  TRY (adhoc_conjunction_tac i));
wenzelm@21687
   383
wenzelm@23418
   384
val recover_conjunction_tac = PRIMITIVE (fn th =>
wenzelm@23418
   385
  Conjunction.uncurry_balanced (Thm.nprems_of th) th);
wenzelm@23414
   386
wenzelm@23414
   387
fun PRECISE_CONJUNCTS n tac =
wenzelm@23414
   388
  SELECT_GOAL (precise_conjunction_tac n 1
wenzelm@23414
   389
    THEN tac
wenzelm@23418
   390
    THEN recover_conjunction_tac);
wenzelm@23414
   391
wenzelm@21687
   392
fun CONJUNCTS tac =
wenzelm@21687
   393
  SELECT_GOAL (conjunction_tac 1
wenzelm@21687
   394
    THEN tac
wenzelm@23418
   395
    THEN recover_conjunction_tac);
wenzelm@21687
   396
wenzelm@21687
   397
wenzelm@21687
   398
(* hhf normal form *)
wenzelm@21687
   399
wenzelm@21687
   400
val norm_hhf_tac =
wenzelm@21687
   401
  rtac Drule.asm_rl  (*cheap approximation -- thanks to builtin Logic.flatten_params*)
wenzelm@21687
   402
  THEN' SUBGOAL (fn (t, i) =>
wenzelm@21687
   403
    if Drule.is_norm_hhf t then all_tac
wenzelm@41228
   404
    else rewrite_goal_tac Drule.norm_hhf_eqs i);
wenzelm@21687
   405
wenzelm@23237
   406
wenzelm@23237
   407
(* non-atomic goal assumptions *)
wenzelm@23237
   408
wenzelm@23356
   409
fun non_atomic (Const ("==>", _) $ _ $ _) = true
wenzelm@23356
   410
  | non_atomic (Const ("all", _) $ _) = true
wenzelm@23356
   411
  | non_atomic _ = false;
wenzelm@23356
   412
wenzelm@23237
   413
fun assume_rule_tac ctxt = norm_hhf_tac THEN' CSUBGOAL (fn (goal, i) =>
wenzelm@23237
   414
  let
wenzelm@42495
   415
    val ((_, goal'), ctxt') = Variable.focus_cterm goal ctxt;
wenzelm@23237
   416
    val goal'' = Drule.cterm_rule (singleton (Variable.export ctxt' ctxt)) goal';
wenzelm@23356
   417
    val Rs = filter (non_atomic o Thm.term_of) (Drule.strip_imp_prems goal'');
wenzelm@23237
   418
    val tacs = Rs |> map (fn R =>
wenzelm@41228
   419
      Tactic.etac (Raw_Simplifier.norm_hhf (Thm.trivial R)) THEN_ALL_NEW assume_tac);
wenzelm@23237
   420
  in fold_rev (curry op APPEND') tacs (K no_tac) i end);
wenzelm@23237
   421
wenzelm@32365
   422
wenzelm@32365
   423
(* parallel tacticals *)
wenzelm@32365
   424
wenzelm@32365
   425
(*parallel choice of single results*)
wenzelm@32365
   426
fun PARALLEL_CHOICE tacs st =
wenzelm@32365
   427
  (case Par_List.get_some (fn tac => SINGLE tac st) tacs of
wenzelm@32365
   428
    NONE => Seq.empty
wenzelm@32365
   429
  | SOME st' => Seq.single st');
wenzelm@32365
   430
wenzelm@32365
   431
(*parallel refinement of non-schematic goal by single results*)
wenzelm@32788
   432
exception FAILED of unit;
wenzelm@42370
   433
fun PARALLEL_GOALS tac =
wenzelm@42370
   434
  Thm.adjust_maxidx_thm ~1 #>
wenzelm@42370
   435
  (fn st =>
wenzelm@42371
   436
    if not (Multithreading.enabled ()) orelse Thm.maxidx_of st >= 0 orelse Thm.nprems_of st <= 1
wenzelm@42370
   437
    then DETERM tac st
wenzelm@42370
   438
    else
wenzelm@42370
   439
      let
wenzelm@42370
   440
        fun try_tac g =
wenzelm@42370
   441
          (case SINGLE tac g of
wenzelm@42370
   442
            NONE => raise FAILED ()
wenzelm@42370
   443
          | SOME g' => g');
wenzelm@32365
   444
wenzelm@42370
   445
        val goals = Drule.strip_imp_prems (Thm.cprop_of st);
wenzelm@42370
   446
        val results = Par_List.map (try_tac o init) goals;
wenzelm@42370
   447
      in ALLGOALS (fn i => retrofit i 1 (nth results (i - 1))) st end
wenzelm@42370
   448
      handle FAILED () => Seq.empty);
wenzelm@32365
   449
wenzelm@18207
   450
end;
wenzelm@18207
   451
wenzelm@32365
   452
structure Basic_Goal: BASIC_GOAL = Goal;
wenzelm@32365
   453
open Basic_Goal;