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