src/Pure/goal.ML
author wenzelm
Sat May 29 19:46:29 2010 +0200 (2010-05-29)
changeset 37186 349e9223c685
parent 36613 f3157c288aca
child 37690 b16231572c61
permissions -rw-r--r--
explicit markup for forked goals, as indicated by Goal.fork;
accumulate pending forks within command state and hilight accordingly;
Isabelle_Process: enforce future_terminal_proof, which gives some impression of non-linear/parallel checking;
     1 (*  Title:      Pure/goal.ML
     2     Author:     Makarius
     3 
     4 Goals in tactical theorem proving.
     5 *)
     6 
     7 signature BASIC_GOAL =
     8 sig
     9   val parallel_proofs: int Unsynchronized.ref
    10   val SELECT_GOAL: tactic -> int -> tactic
    11   val CONJUNCTS: tactic -> int -> tactic
    12   val PRECISE_CONJUNCTS: int -> tactic -> int -> tactic
    13   val PARALLEL_CHOICE: tactic list -> tactic
    14   val PARALLEL_GOALS: tactic -> tactic
    15 end;
    16 
    17 signature GOAL =
    18 sig
    19   include BASIC_GOAL
    20   val init: cterm -> thm
    21   val protect: thm -> thm
    22   val conclude: thm -> thm
    23   val check_finished: Proof.context -> thm -> unit
    24   val finish: Proof.context -> thm -> thm
    25   val norm_result: thm -> thm
    26   val fork: (unit -> 'a) -> 'a future
    27   val future_enabled: unit -> bool
    28   val local_future_enabled: unit -> bool
    29   val local_future_enforced: unit -> bool
    30   val future_result: Proof.context -> thm future -> term -> thm
    31   val prove_internal: cterm list -> cterm -> (thm list -> tactic) -> thm
    32   val prove_multi: Proof.context -> string list -> term list -> term list ->
    33     ({prems: thm list, context: Proof.context} -> tactic) -> thm list
    34   val prove_future: Proof.context -> string list -> term list -> term ->
    35     ({prems: thm list, context: Proof.context} -> tactic) -> thm
    36   val prove: Proof.context -> string list -> term list -> term ->
    37     ({prems: thm list, context: Proof.context} -> tactic) -> thm
    38   val prove_global: theory -> string list -> term list -> term ->
    39     ({prems: thm list, context: Proof.context} -> tactic) -> thm
    40   val extract: int -> int -> thm -> thm Seq.seq
    41   val retrofit: int -> int -> thm -> thm -> thm Seq.seq
    42   val conjunction_tac: int -> tactic
    43   val precise_conjunction_tac: int -> int -> tactic
    44   val recover_conjunction_tac: tactic
    45   val norm_hhf_tac: int -> tactic
    46   val compose_hhf_tac: thm -> int -> tactic
    47   val assume_rule_tac: Proof.context -> int -> tactic
    48 end;
    49 
    50 structure Goal: GOAL =
    51 struct
    52 
    53 (** goals **)
    54 
    55 (*
    56   -------- (init)
    57   C ==> #C
    58 *)
    59 val init =
    60   let val A = #1 (Thm.dest_implies (Thm.cprop_of Drule.protectI))
    61   in fn C => Thm.instantiate ([], [(A, C)]) Drule.protectI end;
    62 
    63 (*
    64    C
    65   --- (protect)
    66   #C
    67 *)
    68 fun protect th = Drule.comp_no_flatten (th, 0) 1 Drule.protectI;
    69 
    70 (*
    71   A ==> ... ==> #C
    72   ---------------- (conclude)
    73   A ==> ... ==> C
    74 *)
    75 fun conclude th = Drule.comp_no_flatten (th, Thm.nprems_of th) 1 Drule.protectD;
    76 
    77 (*
    78   #C
    79   --- (finish)
    80    C
    81 *)
    82 fun check_finished ctxt th =
    83   (case Thm.nprems_of th of
    84     0 => ()
    85   | n => raise THM ("Proof failed.\n" ^
    86       Pretty.string_of (Pretty.chunks
    87         (Goal_Display.pretty_goals ctxt {total = true, main = true, maxgoals = n} th)) ^
    88       "\n" ^ string_of_int n ^ " unsolved goal(s)!", 0, [th]));
    89 
    90 fun finish ctxt th = (check_finished ctxt th; conclude th);
    91 
    92 
    93 
    94 (** results **)
    95 
    96 (* normal form *)
    97 
    98 val norm_result =
    99   Drule.flexflex_unique
   100   #> MetaSimplifier.norm_hhf_protect
   101   #> Thm.strip_shyps
   102   #> Drule.zero_var_indexes;
   103 
   104 
   105 (* parallel proofs *)
   106 
   107 fun fork e = Future.fork_pri ~1 (fn () =>
   108   let
   109     val _ = Output.status (Markup.markup Markup.forked "");
   110     val x = e ();  (*sic*)
   111     val _ = Output.status (Markup.markup Markup.joined "");
   112   in x end);
   113 
   114 val parallel_proofs = Unsynchronized.ref 1;
   115 
   116 fun future_enabled () =
   117   Multithreading.enabled () andalso Future.is_worker () andalso ! parallel_proofs >= 1;
   118 
   119 fun local_future_enabled () = future_enabled () andalso ! parallel_proofs >= 2;
   120 fun local_future_enforced () = future_enabled () andalso ! parallel_proofs >= 3;
   121 
   122 
   123 (* future_result *)
   124 
   125 fun future_result ctxt result prop =
   126   let
   127     val thy = ProofContext.theory_of ctxt;
   128     val _ = Context.reject_draft thy;
   129     val cert = Thm.cterm_of thy;
   130     val certT = Thm.ctyp_of thy;
   131 
   132     val assms = Assumption.all_assms_of ctxt;
   133     val As = map Thm.term_of assms;
   134 
   135     val xs = map Free (fold Term.add_frees (prop :: As) []);
   136     val fixes = map cert xs;
   137 
   138     val tfrees = fold Term.add_tfrees (prop :: As) [];
   139     val instT = map (fn (a, S) => (certT (TVar ((a, 0), S)), certT (TFree (a, S)))) tfrees;
   140 
   141     val global_prop =
   142       cert (Term.map_types Logic.varifyT_global
   143         (fold_rev Logic.all xs (Logic.list_implies (As, prop))))
   144       |> Thm.weaken_sorts (Variable.sorts_of ctxt);
   145     val global_result = result |> Future.map
   146       (Drule.flexflex_unique #>
   147         Thm.adjust_maxidx_thm ~1 #>
   148         Drule.implies_intr_list assms #>
   149         Drule.forall_intr_list fixes #>
   150         Thm.generalize (map #1 tfrees, []) 0 #>
   151         Thm.strip_shyps);
   152     val local_result =
   153       Thm.future global_result global_prop
   154       |> Thm.instantiate (instT, [])
   155       |> Drule.forall_elim_list fixes
   156       |> fold (Thm.elim_implies o Thm.assume) assms;
   157   in local_result end;
   158 
   159 
   160 
   161 (** tactical theorem proving **)
   162 
   163 (* prove_internal -- minimal checks, no normalization of result! *)
   164 
   165 fun prove_internal casms cprop tac =
   166   (case SINGLE (tac (map Assumption.assume casms)) (init cprop) of
   167     SOME th => Drule.implies_intr_list casms
   168       (finish (Syntax.init_pretty_global (Thm.theory_of_thm th)) th)
   169   | NONE => error "Tactic failed.");
   170 
   171 
   172 (* prove_common etc. *)
   173 
   174 fun prove_common immediate ctxt xs asms props tac =
   175   let
   176     val thy = ProofContext.theory_of ctxt;
   177     val string_of_term = Syntax.string_of_term ctxt;
   178 
   179     val pos = Position.thread_data ();
   180     fun err msg = cat_error msg
   181       ("The error(s) above occurred for the goal statement:\n" ^
   182         string_of_term (Logic.list_implies (asms, Logic.mk_conjunction_list props)) ^
   183         (case Position.str_of pos of "" => "" | s => "\n" ^ s));
   184 
   185     fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm (Term.no_dummy_patterns t))
   186       handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
   187     val casms = map cert_safe asms;
   188     val cprops = map cert_safe props;
   189 
   190     val (prems, ctxt') = ctxt
   191       |> Variable.add_fixes_direct xs
   192       |> fold Variable.declare_term (asms @ props)
   193       |> Assumption.add_assumes casms
   194       ||> Variable.set_body true;
   195     val sorts = Variable.sorts_of ctxt';
   196 
   197     val stmt = Thm.weaken_sorts sorts (Conjunction.mk_conjunction_balanced cprops);
   198 
   199     fun result () =
   200       (case SINGLE (tac {prems = prems, context = ctxt'}) (init stmt) of
   201         NONE => err "Tactic failed."
   202       | SOME st =>
   203           let val res = finish ctxt' st handle THM (msg, _, _) => err msg in
   204             if Unify.matches_list thy [Thm.term_of stmt] [Thm.prop_of res]
   205             then Thm.check_shyps sorts res
   206             else err ("Proved a different theorem: " ^ string_of_term (Thm.prop_of res))
   207           end);
   208     val res =
   209       if immediate orelse #maxidx (Thm.rep_cterm stmt) >= 0 orelse not (future_enabled ())
   210       then result ()
   211       else future_result ctxt' (fork result) (Thm.term_of stmt);
   212   in
   213     Conjunction.elim_balanced (length props) res
   214     |> map (Assumption.export false ctxt' ctxt)
   215     |> Variable.export ctxt' ctxt
   216     |> map Drule.zero_var_indexes
   217   end;
   218 
   219 val prove_multi = prove_common true;
   220 
   221 fun prove_future ctxt xs asms prop tac = hd (prove_common false ctxt xs asms [prop] tac);
   222 fun prove ctxt xs asms prop tac = hd (prove_common true ctxt xs asms [prop] tac);
   223 
   224 fun prove_global thy xs asms prop tac =
   225   Drule.export_without_context (prove (ProofContext.init_global thy) xs asms prop tac);
   226 
   227 
   228 
   229 (** goal structure **)
   230 
   231 (* nested goals *)
   232 
   233 fun extract i n st =
   234   (if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st then Seq.empty
   235    else if n = 1 then Seq.single (Thm.cprem_of st i)
   236    else
   237      Seq.single (Conjunction.mk_conjunction_balanced (map (Thm.cprem_of st) (i upto i + n - 1))))
   238   |> Seq.map (Thm.adjust_maxidx_cterm ~1 #> init);
   239 
   240 fun retrofit i n st' st =
   241   (if n = 1 then st
   242    else st |> Drule.with_subgoal i (Conjunction.uncurry_balanced n))
   243   |> Thm.compose_no_flatten false (conclude st', Thm.nprems_of st') i;
   244 
   245 fun SELECT_GOAL tac i st =
   246   if Thm.nprems_of st = 1 andalso i = 1 then tac st
   247   else Seq.lifts (retrofit i 1) (Seq.maps tac (extract i 1 st)) st;
   248 
   249 
   250 (* multiple goals *)
   251 
   252 fun precise_conjunction_tac 0 i = eq_assume_tac i
   253   | precise_conjunction_tac 1 i = SUBGOAL (K all_tac) i
   254   | precise_conjunction_tac n i = PRIMITIVE (Drule.with_subgoal i (Conjunction.curry_balanced n));
   255 
   256 val adhoc_conjunction_tac = REPEAT_ALL_NEW
   257   (SUBGOAL (fn (goal, i) =>
   258     if can Logic.dest_conjunction goal then rtac Conjunction.conjunctionI i
   259     else no_tac));
   260 
   261 val conjunction_tac = SUBGOAL (fn (goal, i) =>
   262   precise_conjunction_tac (length (Logic.dest_conjunctions goal)) i ORELSE
   263   TRY (adhoc_conjunction_tac i));
   264 
   265 val recover_conjunction_tac = PRIMITIVE (fn th =>
   266   Conjunction.uncurry_balanced (Thm.nprems_of th) th);
   267 
   268 fun PRECISE_CONJUNCTS n tac =
   269   SELECT_GOAL (precise_conjunction_tac n 1
   270     THEN tac
   271     THEN recover_conjunction_tac);
   272 
   273 fun CONJUNCTS tac =
   274   SELECT_GOAL (conjunction_tac 1
   275     THEN tac
   276     THEN recover_conjunction_tac);
   277 
   278 
   279 (* hhf normal form *)
   280 
   281 val norm_hhf_tac =
   282   rtac Drule.asm_rl  (*cheap approximation -- thanks to builtin Logic.flatten_params*)
   283   THEN' SUBGOAL (fn (t, i) =>
   284     if Drule.is_norm_hhf t then all_tac
   285     else MetaSimplifier.rewrite_goal_tac Drule.norm_hhf_eqs i);
   286 
   287 fun compose_hhf_tac th i st =
   288   PRIMSEQ (Thm.bicompose false (false, Drule.lift_all (Thm.cprem_of st i) th, 0) i) st;
   289 
   290 
   291 (* non-atomic goal assumptions *)
   292 
   293 fun non_atomic (Const ("==>", _) $ _ $ _) = true
   294   | non_atomic (Const ("all", _) $ _) = true
   295   | non_atomic _ = false;
   296 
   297 fun assume_rule_tac ctxt = norm_hhf_tac THEN' CSUBGOAL (fn (goal, i) =>
   298   let
   299     val ((_, goal'), ctxt') = Variable.focus goal ctxt;
   300     val goal'' = Drule.cterm_rule (singleton (Variable.export ctxt' ctxt)) goal';
   301     val Rs = filter (non_atomic o Thm.term_of) (Drule.strip_imp_prems goal'');
   302     val tacs = Rs |> map (fn R =>
   303       Tactic.etac (MetaSimplifier.norm_hhf (Thm.trivial R)) THEN_ALL_NEW assume_tac);
   304   in fold_rev (curry op APPEND') tacs (K no_tac) i end);
   305 
   306 
   307 (* parallel tacticals *)
   308 
   309 (*parallel choice of single results*)
   310 fun PARALLEL_CHOICE tacs st =
   311   (case Par_List.get_some (fn tac => SINGLE tac st) tacs of
   312     NONE => Seq.empty
   313   | SOME st' => Seq.single st');
   314 
   315 (*parallel refinement of non-schematic goal by single results*)
   316 exception FAILED of unit;
   317 fun PARALLEL_GOALS tac st =
   318   let
   319     val st0 = Thm.adjust_maxidx_thm ~1 st;
   320     val _ = Thm.maxidx_of st0 >= 0 andalso
   321       raise THM ("PARALLEL_GOALS: schematic goal state", 0, [st]);
   322 
   323     fun try_tac g =
   324       (case SINGLE tac g of
   325         NONE => raise FAILED ()
   326       | SOME g' => g');
   327 
   328     val goals = Drule.strip_imp_prems (Thm.cprop_of st0);
   329     val results = Par_List.map (try_tac o init) goals;
   330   in ALLGOALS (fn i => retrofit i 1 (nth results (i - 1))) st0 end
   331   handle FAILED () => Seq.empty;
   332 
   333 end;
   334 
   335 structure Basic_Goal: BASIC_GOAL = Goal;
   336 open Basic_Goal;