src/Pure/goal.ML
author wenzelm
Wed Jul 31 13:00:42 2013 +0200 (2013-07-31)
changeset 52811 dae6c61f991e
parent 52775 e0169f13bd37
child 53189 ee8b8dafef0e
permissions -rw-r--r--
clarified priority of "skipped" proofs, which might take long but do not produce relevant information (potential conflict of quick interactive feedback vs. performance in batch mode);
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@52765
    30
  val purge_futures: int list -> unit
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@52764
   122
    (0, Inttab.empty: (Future.group * unit future) list Inttab.table);
wenzelm@49061
   123
wenzelm@49061
   124
fun count_forked i =
wenzelm@52764
   125
  Synchronized.change forked_proofs (fn (m, tab) =>
wenzelm@41703
   126
    let
wenzelm@41703
   127
      val n = m + i;
wenzelm@50974
   128
      val _ = Future.forked_proofs := n;
wenzelm@52764
   129
    in (n, tab) end);
wenzelm@49061
   130
wenzelm@49061
   131
fun register_forked id future =
wenzelm@52764
   132
  Synchronized.change forked_proofs (fn (m, tab) =>
wenzelm@49061
   133
    let
wenzelm@52764
   134
      val group = Task_Queue.group_of_task (Future.task_of future);
wenzelm@52764
   135
      val tab' = Inttab.cons_list (id, (group, Future.map (K ()) future)) tab;
wenzelm@52764
   136
    in (m, 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@52764
   182
  map #2 (Inttab.lookup_list (#2 (Synchronized.value forked_proofs)) id);
wenzelm@49061
   183
wenzelm@52775
   184
fun check_canceled id group =
wenzelm@52775
   185
  if Task_Queue.is_canceled group then ()
wenzelm@52775
   186
  else raise Fail ("Attempt to purge valid execution " ^ string_of_int id);
wenzelm@52775
   187
wenzelm@52765
   188
fun purge_futures ids =
wenzelm@52765
   189
  Synchronized.change forked_proofs (fn (_, tab) =>
wenzelm@52765
   190
    let
wenzelm@52765
   191
      val tab' = fold Inttab.delete_safe ids tab;
wenzelm@52775
   192
      val n' =
wenzelm@52775
   193
        Inttab.fold (fn (id, futures) => fn m =>
wenzelm@52775
   194
          if Inttab.defined tab' id then m + length futures
wenzelm@52775
   195
          else (List.app (check_canceled id o #1) futures; m)) tab 0;
wenzelm@52765
   196
      val _ = Future.forked_proofs := n';
wenzelm@52765
   197
    in (n', tab') end);
wenzelm@52765
   198
wenzelm@49931
   199
fun reset_futures () =
wenzelm@52764
   200
  Synchronized.change_result forked_proofs (fn (_, tab) =>
wenzelm@52764
   201
    let
wenzelm@52764
   202
      val groups = Inttab.fold (fold (cons o #1) o #2) tab [];
wenzelm@52764
   203
      val _ = Future.forked_proofs := 0;
wenzelm@52764
   204
    in (groups, (0, Inttab.empty)) end);
wenzelm@49061
   205
wenzelm@51276
   206
fun shutdown_futures () =
wenzelm@51276
   207
  (Future.shutdown ();
wenzelm@51276
   208
    (case map_filter Task_Queue.group_status (reset_futures ()) of
wenzelm@51276
   209
      [] => ()
wenzelm@51276
   210
    | exns => raise Par_Exn.make exns));
wenzelm@51276
   211
wenzelm@49061
   212
end;
wenzelm@49061
   213
wenzelm@41677
   214
wenzelm@41703
   215
(* scheduling parameters *)
wenzelm@41703
   216
wenzelm@52499
   217
fun skip_proofs_enabled () =
wenzelm@52710
   218
  let val skip = Options.default_bool "skip_proofs" in
wenzelm@52499
   219
    if Proofterm.proofs_enabled () andalso skip then
wenzelm@52499
   220
      (warning "Proof terms enabled -- cannot skip proofs"; false)
wenzelm@52499
   221
    else skip
wenzelm@52499
   222
  end;
wenzelm@52499
   223
wenzelm@32738
   224
val parallel_proofs = Unsynchronized.ref 1;
wenzelm@29448
   225
wenzelm@49012
   226
fun future_enabled_level n =
wenzelm@49012
   227
  Multithreading.enabled () andalso ! parallel_proofs >= n andalso
wenzelm@41703
   228
  is_some (Future.worker_task ());
wenzelm@32061
   229
wenzelm@49012
   230
fun future_enabled () = future_enabled_level 1;
wenzelm@49012
   231
wenzelm@49012
   232
fun future_enabled_nested n =
wenzelm@49012
   233
  future_enabled_level n andalso
wenzelm@52104
   234
  forked_count () <
wenzelm@52104
   235
    Options.default_int "parallel_subproofs_saturation" * Multithreading.max_threads_value ();
wenzelm@51423
   236
wenzelm@51423
   237
fun future_enabled_timing t =
wenzelm@52104
   238
  future_enabled () andalso
wenzelm@52104
   239
    Time.toReal t >= Options.default_real "parallel_subproofs_threshold";
wenzelm@29448
   240
wenzelm@29448
   241
wenzelm@28446
   242
(* future_result *)
wenzelm@28340
   243
wenzelm@28446
   244
fun future_result ctxt result prop =
wenzelm@28340
   245
  let
wenzelm@42360
   246
    val thy = Proof_Context.theory_of ctxt;
wenzelm@28340
   247
    val cert = Thm.cterm_of thy;
wenzelm@28340
   248
    val certT = Thm.ctyp_of thy;
wenzelm@28340
   249
wenzelm@30473
   250
    val assms = Assumption.all_assms_of ctxt;
wenzelm@28340
   251
    val As = map Thm.term_of assms;
wenzelm@28340
   252
wenzelm@28340
   253
    val xs = map Free (fold Term.add_frees (prop :: As) []);
wenzelm@28340
   254
    val fixes = map cert xs;
wenzelm@28340
   255
wenzelm@28340
   256
    val tfrees = fold Term.add_tfrees (prop :: As) [];
wenzelm@28340
   257
    val instT = map (fn (a, S) => (certT (TVar ((a, 0), S)), certT (TFree (a, S)))) tfrees;
wenzelm@28340
   258
wenzelm@28340
   259
    val global_prop =
wenzelm@45344
   260
      cert (Logic.varify_types_global (fold_rev Logic.all xs (Logic.list_implies (As, prop))))
wenzelm@32056
   261
      |> Thm.weaken_sorts (Variable.sorts_of ctxt);
wenzelm@28973
   262
    val global_result = result |> Future.map
wenzelm@33768
   263
      (Drule.flexflex_unique #>
wenzelm@33768
   264
        Thm.adjust_maxidx_thm ~1 #>
wenzelm@28973
   265
        Drule.implies_intr_list assms #>
wenzelm@28973
   266
        Drule.forall_intr_list fixes #>
wenzelm@36613
   267
        Thm.generalize (map #1 tfrees, []) 0 #>
wenzelm@36613
   268
        Thm.strip_shyps);
wenzelm@28340
   269
    val local_result =
wenzelm@32056
   270
      Thm.future global_result global_prop
wenzelm@50987
   271
      |> Thm.close_derivation
wenzelm@28340
   272
      |> Thm.instantiate (instT, [])
wenzelm@28340
   273
      |> Drule.forall_elim_list fixes
wenzelm@28340
   274
      |> fold (Thm.elim_implies o Thm.assume) assms;
wenzelm@28340
   275
  in local_result end;
wenzelm@28340
   276
wenzelm@28340
   277
wenzelm@18027
   278
wenzelm@18027
   279
(** tactical theorem proving **)
wenzelm@18027
   280
wenzelm@23356
   281
(* prove_internal -- minimal checks, no normalization of result! *)
wenzelm@20250
   282
wenzelm@23356
   283
fun prove_internal casms cprop tac =
wenzelm@20250
   284
  (case SINGLE (tac (map Assumption.assume casms)) (init cprop) of
wenzelm@32197
   285
    SOME th => Drule.implies_intr_list casms
wenzelm@32197
   286
      (finish (Syntax.init_pretty_global (Thm.theory_of_thm th)) th)
wenzelm@38875
   287
  | NONE => error "Tactic failed");
wenzelm@20250
   288
wenzelm@20250
   289
wenzelm@51553
   290
(* prove variations *)
wenzelm@51553
   291
wenzelm@51553
   292
fun is_schematic t =
wenzelm@51553
   293
  Term.exists_subterm Term.is_Var t orelse
wenzelm@51553
   294
  Term.exists_type (Term.exists_subtype Term.is_TVar) t;
wenzelm@17986
   295
wenzelm@52811
   296
fun prove_common immediate pri ctxt xs asms props tac =
wenzelm@17980
   297
  let
wenzelm@42360
   298
    val thy = Proof_Context.theory_of ctxt;
wenzelm@26939
   299
    val string_of_term = Syntax.string_of_term ctxt;
wenzelm@20056
   300
wenzelm@51553
   301
    val schematic = exists is_schematic props;
wenzelm@51553
   302
    val future = future_enabled ();
wenzelm@52499
   303
    val skip = not immediate andalso not schematic andalso future andalso skip_proofs_enabled ();
wenzelm@51553
   304
wenzelm@28355
   305
    val pos = Position.thread_data ();
wenzelm@20250
   306
    fun err msg = cat_error msg
wenzelm@20250
   307
      ("The error(s) above occurred for the goal statement:\n" ^
wenzelm@28355
   308
        string_of_term (Logic.list_implies (asms, Logic.mk_conjunction_list props)) ^
wenzelm@48992
   309
        (case Position.here pos of "" => "" | s => "\n" ^ s));
wenzelm@17980
   310
wenzelm@20250
   311
    fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm (Term.no_dummy_patterns t))
wenzelm@17980
   312
      handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
wenzelm@20250
   313
    val casms = map cert_safe asms;
wenzelm@20250
   314
    val cprops = map cert_safe props;
wenzelm@17980
   315
wenzelm@20250
   316
    val (prems, ctxt') = ctxt
wenzelm@20250
   317
      |> Variable.add_fixes_direct xs
wenzelm@27218
   318
      |> fold Variable.declare_term (asms @ props)
wenzelm@26713
   319
      |> Assumption.add_assumes casms
wenzelm@26713
   320
      ||> Variable.set_body true;
wenzelm@28619
   321
    val sorts = Variable.sorts_of ctxt';
wenzelm@17980
   322
wenzelm@28619
   323
    val stmt = Thm.weaken_sorts sorts (Conjunction.mk_conjunction_balanced cprops);
wenzelm@28340
   324
wenzelm@51553
   325
    fun tac' args st =
wenzelm@51553
   326
      if skip then ALLGOALS Skip_Proof.cheat_tac st before Skip_Proof.report ctxt
wenzelm@51553
   327
      else tac args st;
wenzelm@28340
   328
    fun result () =
wenzelm@51553
   329
      (case SINGLE (tac' {prems = prems, context = ctxt'}) (init stmt) of
wenzelm@38875
   330
        NONE => err "Tactic failed"
wenzelm@28340
   331
      | SOME st =>
wenzelm@32197
   332
          let val res = finish ctxt' st handle THM (msg, _, _) => err msg in
wenzelm@28619
   333
            if Unify.matches_list thy [Thm.term_of stmt] [Thm.prop_of res]
wenzelm@28619
   334
            then Thm.check_shyps sorts res
wenzelm@28340
   335
            else err ("Proved a different theorem: " ^ string_of_term (Thm.prop_of res))
wenzelm@28340
   336
          end);
wenzelm@28340
   337
    val res =
wenzelm@51553
   338
      if immediate orelse schematic orelse not future orelse skip
wenzelm@29088
   339
      then result ()
wenzelm@52811
   340
      else future_result ctxt' (fork pri result) (Thm.term_of stmt);
wenzelm@17980
   341
  in
wenzelm@28340
   342
    Conjunction.elim_balanced (length props) res
wenzelm@20290
   343
    |> map (Assumption.export false ctxt' ctxt)
wenzelm@20056
   344
    |> Variable.export ctxt' ctxt
wenzelm@20250
   345
    |> map Drule.zero_var_indexes
wenzelm@17980
   346
  end;
wenzelm@17980
   347
wenzelm@52811
   348
val prove_multi = prove_common true 0;
wenzelm@17980
   349
wenzelm@52811
   350
fun prove_future_pri pri ctxt xs asms prop tac =
wenzelm@52811
   351
  hd (prove_common false pri ctxt xs asms [prop] tac);
wenzelm@51118
   352
wenzelm@52811
   353
val prove_future = prove_future_pri ~1;
wenzelm@52811
   354
wenzelm@52811
   355
fun prove ctxt xs asms prop tac = hd (prove_multi ctxt xs asms [prop] tac);
wenzelm@20056
   356
wenzelm@51118
   357
fun prove_global_future thy xs asms prop tac =
wenzelm@51118
   358
  Drule.export_without_context (prove_future (Proof_Context.init_global thy) xs asms prop tac);
wenzelm@51118
   359
wenzelm@20056
   360
fun prove_global thy xs asms prop tac =
wenzelm@42360
   361
  Drule.export_without_context (prove (Proof_Context.init_global thy) xs asms prop tac);
wenzelm@18027
   362
wenzelm@51551
   363
fun prove_sorry ctxt xs asms prop tac =
wenzelm@52059
   364
  if Config.get ctxt quick_and_dirty then
wenzelm@51552
   365
    prove ctxt xs asms prop (fn _ => ALLGOALS Skip_Proof.cheat_tac)
wenzelm@52811
   366
  else (if future_enabled () then prove_future_pri ~2 else prove) ctxt xs asms prop tac;
wenzelm@51551
   367
wenzelm@51551
   368
fun prove_sorry_global thy xs asms prop tac =
wenzelm@51551
   369
  Drule.export_without_context
wenzelm@51551
   370
    (prove_sorry (Proof_Context.init_global thy) xs asms prop tac);
wenzelm@51551
   371
wenzelm@18027
   372
wenzelm@17980
   373
wenzelm@21687
   374
(** goal structure **)
wenzelm@21687
   375
wenzelm@52458
   376
(* rearrange subgoals *)
wenzelm@52458
   377
wenzelm@52458
   378
fun restrict i n st =
wenzelm@52458
   379
  if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st
wenzelm@52458
   380
  then raise THM ("Goal.restrict", i, [st])
wenzelm@52458
   381
  else rotate_prems (i - 1) st |> protect n;
wenzelm@52458
   382
wenzelm@52458
   383
fun unrestrict i = conclude #> rotate_prems (1 - i);
wenzelm@52458
   384
wenzelm@52458
   385
(*with structural marker*)
wenzelm@17980
   386
fun SELECT_GOAL tac i st =
wenzelm@19191
   387
  if Thm.nprems_of st = 1 andalso i = 1 then tac st
wenzelm@52458
   388
  else (PRIMITIVE (restrict i 1) THEN tac THEN PRIMITIVE (unrestrict i)) st;
wenzelm@52458
   389
wenzelm@52458
   390
(*without structural marker*)
wenzelm@52458
   391
fun PREFER_GOAL tac i st =
wenzelm@52458
   392
  if i < 1 orelse i > Thm.nprems_of st then Seq.empty
wenzelm@52458
   393
  else (PRIMITIVE (rotate_prems (i - 1)) THEN tac THEN PRIMITIVE (rotate_prems (1 - i))) st;
wenzelm@17980
   394
wenzelm@21687
   395
wenzelm@21687
   396
(* multiple goals *)
wenzelm@21687
   397
wenzelm@23418
   398
fun precise_conjunction_tac 0 i = eq_assume_tac i
wenzelm@23418
   399
  | precise_conjunction_tac 1 i = SUBGOAL (K all_tac) i
wenzelm@23418
   400
  | precise_conjunction_tac n i = PRIMITIVE (Drule.with_subgoal i (Conjunction.curry_balanced n));
wenzelm@23414
   401
wenzelm@23418
   402
val adhoc_conjunction_tac = REPEAT_ALL_NEW
wenzelm@23418
   403
  (SUBGOAL (fn (goal, i) =>
wenzelm@23418
   404
    if can Logic.dest_conjunction goal then rtac Conjunction.conjunctionI i
wenzelm@23418
   405
    else no_tac));
wenzelm@21687
   406
wenzelm@23418
   407
val conjunction_tac = SUBGOAL (fn (goal, i) =>
wenzelm@23418
   408
  precise_conjunction_tac (length (Logic.dest_conjunctions goal)) i ORELSE
wenzelm@23418
   409
  TRY (adhoc_conjunction_tac i));
wenzelm@21687
   410
wenzelm@23418
   411
val recover_conjunction_tac = PRIMITIVE (fn th =>
wenzelm@23418
   412
  Conjunction.uncurry_balanced (Thm.nprems_of th) th);
wenzelm@23414
   413
wenzelm@23414
   414
fun PRECISE_CONJUNCTS n tac =
wenzelm@23414
   415
  SELECT_GOAL (precise_conjunction_tac n 1
wenzelm@23414
   416
    THEN tac
wenzelm@23418
   417
    THEN recover_conjunction_tac);
wenzelm@23414
   418
wenzelm@21687
   419
fun CONJUNCTS tac =
wenzelm@21687
   420
  SELECT_GOAL (conjunction_tac 1
wenzelm@21687
   421
    THEN tac
wenzelm@23418
   422
    THEN recover_conjunction_tac);
wenzelm@21687
   423
wenzelm@21687
   424
wenzelm@21687
   425
(* hhf normal form *)
wenzelm@21687
   426
wenzelm@21687
   427
val norm_hhf_tac =
wenzelm@21687
   428
  rtac Drule.asm_rl  (*cheap approximation -- thanks to builtin Logic.flatten_params*)
wenzelm@21687
   429
  THEN' SUBGOAL (fn (t, i) =>
wenzelm@21687
   430
    if Drule.is_norm_hhf t then all_tac
wenzelm@41228
   431
    else rewrite_goal_tac Drule.norm_hhf_eqs i);
wenzelm@21687
   432
wenzelm@23237
   433
wenzelm@23237
   434
(* non-atomic goal assumptions *)
wenzelm@23237
   435
wenzelm@23356
   436
fun non_atomic (Const ("==>", _) $ _ $ _) = true
wenzelm@23356
   437
  | non_atomic (Const ("all", _) $ _) = true
wenzelm@23356
   438
  | non_atomic _ = false;
wenzelm@23356
   439
wenzelm@23237
   440
fun assume_rule_tac ctxt = norm_hhf_tac THEN' CSUBGOAL (fn (goal, i) =>
wenzelm@23237
   441
  let
wenzelm@42495
   442
    val ((_, goal'), ctxt') = Variable.focus_cterm goal ctxt;
wenzelm@23237
   443
    val goal'' = Drule.cterm_rule (singleton (Variable.export ctxt' ctxt)) goal';
wenzelm@23356
   444
    val Rs = filter (non_atomic o Thm.term_of) (Drule.strip_imp_prems goal'');
wenzelm@23237
   445
    val tacs = Rs |> map (fn R =>
wenzelm@52732
   446
      etac (Raw_Simplifier.norm_hhf (Thm.trivial R)) THEN_ALL_NEW assume_tac);
wenzelm@23237
   447
  in fold_rev (curry op APPEND') tacs (K no_tac) i end);
wenzelm@23237
   448
wenzelm@32365
   449
wenzelm@52461
   450
wenzelm@52461
   451
(** parallel tacticals **)
wenzelm@32365
   452
wenzelm@52461
   453
(* parallel choice of single results *)
wenzelm@52461
   454
wenzelm@32365
   455
fun PARALLEL_CHOICE tacs st =
wenzelm@32365
   456
  (case Par_List.get_some (fn tac => SINGLE tac st) tacs of
wenzelm@32365
   457
    NONE => Seq.empty
wenzelm@32365
   458
  | SOME st' => Seq.single st');
wenzelm@32365
   459
wenzelm@52461
   460
wenzelm@52461
   461
(* parallel refinement of non-schematic goal by single results *)
wenzelm@52461
   462
wenzelm@52461
   463
local
wenzelm@52461
   464
wenzelm@32788
   465
exception FAILED of unit;
wenzelm@52461
   466
wenzelm@52461
   467
fun retrofit st' =
wenzelm@52461
   468
  rotate_prems ~1 #>
wenzelm@52461
   469
  Thm.bicompose {flatten = false, match = false, incremented = false}
wenzelm@52461
   470
      (false, conclude st', Thm.nprems_of st') 1;
wenzelm@52461
   471
wenzelm@52461
   472
in
wenzelm@52461
   473
wenzelm@42370
   474
fun PARALLEL_GOALS tac =
wenzelm@42370
   475
  Thm.adjust_maxidx_thm ~1 #>
wenzelm@42370
   476
  (fn st =>
wenzelm@42371
   477
    if not (Multithreading.enabled ()) orelse Thm.maxidx_of st >= 0 orelse Thm.nprems_of st <= 1
wenzelm@42370
   478
    then DETERM tac st
wenzelm@42370
   479
    else
wenzelm@42370
   480
      let
wenzelm@42370
   481
        fun try_tac g =
wenzelm@42370
   482
          (case SINGLE tac g of
wenzelm@42370
   483
            NONE => raise FAILED ()
wenzelm@42370
   484
          | SOME g' => g');
wenzelm@32365
   485
wenzelm@42370
   486
        val goals = Drule.strip_imp_prems (Thm.cprop_of st);
wenzelm@42370
   487
        val results = Par_List.map (try_tac o init) goals;
wenzelm@52461
   488
      in EVERY (map retrofit (rev results)) st end
wenzelm@42370
   489
      handle FAILED () => Seq.empty);
wenzelm@32365
   490
wenzelm@18207
   491
end;
wenzelm@18207
   492
wenzelm@52461
   493
end;
wenzelm@52461
   494
wenzelm@32365
   495
structure Basic_Goal: BASIC_GOAL = Goal;
wenzelm@32365
   496
open Basic_Goal;