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