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