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