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