src/Pure/goal.ML
author wenzelm
Fri Oct 28 22:27:55 2005 +0200 (2005-10-28)
changeset 18027 09ab79d4e8e1
parent 17986 0450847646c3
child 18119 63901a9b99dc
permissions -rw-r--r--
renamed Goal constant to prop, more general protect/unprotect interfaces;
renamed norm_hhf_rule to norm_hhf;
added comp_hhf, compose_hhf_tac, based on Drule.lift_all;
     1 (*  Title:      Pure/goal.ML
     2     ID:         $Id$
     3     Author:     Makarius and Lawrence C Paulson
     4 
     5 Tactical goal state.
     6 *)
     7 
     8 signature BASIC_GOAL =
     9 sig
    10   val SELECT_GOAL: tactic -> int -> tactic
    11 end;
    12 
    13 signature GOAL =
    14 sig
    15   include BASIC_GOAL
    16   val init: cterm -> thm
    17   val protect: thm -> thm
    18   val conclude: thm -> thm
    19   val finish: thm -> thm
    20   val norm_hhf: thm -> thm
    21   val comp_hhf: thm -> thm -> thm
    22   val compose_hhf_tac: thm list -> int -> tactic
    23   val prove: theory -> string list -> term list -> term -> (thm list -> tactic) -> thm
    24   val prove_multi: theory -> string list -> term list -> term list ->
    25     (thm list -> tactic) -> thm list
    26   val prove_raw: cterm list -> cterm -> (thm list -> tactic) -> thm
    27 
    28   (* FIXME remove *)
    29   val norm_hhf_plain: thm -> thm
    30   val prove_multi_plain: theory -> string list -> term list -> term list ->
    31     (thm list -> tactic) -> thm list
    32   val prove_plain: theory -> string list -> term list -> term -> (thm list -> tactic) -> thm
    33 end;
    34 
    35 structure Goal: GOAL =
    36 struct
    37 
    38 (** goals **)
    39 
    40 (*
    41   -------- (init)
    42   C ==> #C
    43 *)
    44 fun init ct = Drule.instantiate' [] [SOME ct] Drule.protectI;
    45 
    46 (*
    47     A ==> ... ==> C
    48   ------------------ (protect)
    49   #(A ==> ... ==> C)
    50 *)
    51 fun protect th = th COMP Drule.incr_indexes th Drule.protectI;
    52 
    53 (*
    54   A ==> ... ==> #C
    55   ---------------- (conclude)
    56   A ==> ... ==> C
    57 *)
    58 fun conclude th =
    59   (case SINGLE (Thm.bicompose false (false, th, Thm.nprems_of th) 1)
    60       (Drule.incr_indexes th Drule.protectD) of
    61     SOME th' => th'
    62   | NONE => raise THM ("Failed to conclude goal", 0, [th]));
    63 
    64 (*
    65   #C
    66   --- (finish)
    67    C
    68 *)
    69 fun finish th =
    70   (case Thm.nprems_of th of
    71     0 => conclude th
    72   | n => raise THM ("Proof failed.\n" ^
    73       Pretty.string_of (Pretty.chunks (Display.pretty_goals n th)) ^
    74       ("\n" ^ string_of_int n ^ " unsolved goal(s)!"), 0, [th]));
    75 
    76 
    77 
    78 (** results **)
    79 
    80 (* HHF normal form *)
    81 
    82 val norm_hhf_plain =  (* FIXME remove *)
    83   (not o Drule.is_norm_hhf o Thm.prop_of) ?
    84     MetaSimplifier.simplify_aux (K (K NONE)) true [Drule.norm_hhf_eq];
    85 
    86 val norm_hhf =
    87   norm_hhf_plain
    88   #> Thm.adjust_maxidx_thm
    89   #> Drule.gen_all;
    90 
    91 
    92 (* composition of normal results *)
    93 
    94 fun compose_hhf tha i thb =
    95   Thm.bicompose false (false, Drule.lift_all (Thm.cgoal_of thb i) tha, 0) i thb;
    96 
    97 fun comp_hhf tha thb =
    98   (case Seq.pull (compose_hhf tha 1 thb) of
    99     SOME (th, _) => th
   100   | NONE => raise THM ("comp_hhf", 1, [tha, thb]));
   101 
   102 fun compose_hhf_tac [] _ = no_tac
   103   | compose_hhf_tac (th :: ths) i = PRIMSEQ (compose_hhf th i) APPEND compose_hhf_tac ths i;
   104 
   105 
   106 
   107 (** tactical theorem proving **)
   108 
   109 (* prove *)
   110 
   111 local
   112 
   113 fun gen_prove finish_thm thy xs asms props tac =
   114   let
   115     val prop = Logic.mk_conjunction_list props;
   116     val statement = Logic.list_implies (asms, prop);
   117     val frees = map Term.dest_Free (Term.term_frees statement);
   118     val fixed_frees = filter_out (member (op =) xs o #1) frees;
   119     val fixed_tfrees = foldr Term.add_typ_tfrees [] (map #2 fixed_frees);
   120     val params = List.mapPartial (fn x => Option.map (pair x) (AList.lookup (op =) frees x)) xs;
   121 
   122     fun err msg = raise ERROR_MESSAGE
   123       (msg ^ "\nThe error(s) above occurred for the goal statement:\n" ^
   124         Sign.string_of_term thy (Term.list_all_free (params, statement)));
   125 
   126     fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm t)
   127       handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
   128 
   129     val _ = cert_safe statement;
   130     val _ = Term.no_dummy_patterns statement handle TERM (msg, _) => err msg;
   131 
   132     val cparams = map (cert_safe o Free) params;
   133     val casms = map cert_safe asms;
   134     val prems = map (norm_hhf o Thm.assume) casms;
   135 
   136     val goal = init (cert_safe prop);
   137     val goal' = (case SINGLE (tac prems) goal of SOME goal' => goal' | _ => err "Tactic failed.");
   138     val raw_result = finish goal' handle THM (msg, _, _) => err msg;
   139 
   140     val prop' = Thm.prop_of raw_result;
   141     val _ = conditional (not (Pattern.matches thy (Envir.beta_norm prop, prop'))) (fn () =>
   142       err ("Proved a different theorem: " ^ Sign.string_of_term thy prop'));
   143   in
   144     Drule.conj_elim_precise (length props) raw_result
   145     |> map
   146       (Drule.implies_intr_list casms
   147         #> Drule.forall_intr_list cparams
   148         #> finish_thm fixed_tfrees)
   149   end;
   150 
   151 in
   152 
   153 fun prove_multi thy xs asms prop tac =
   154   gen_prove (fn fixed_tfrees => Drule.zero_var_indexes o
   155       (#1 o Thm.varifyT' fixed_tfrees) o norm_hhf)
   156     thy xs asms prop tac;
   157 
   158 fun prove thy xs asms prop tac = hd (prove_multi thy xs asms [prop] tac);
   159 
   160 fun prove_multi_plain thy xs asms prop tac = gen_prove (K norm_hhf_plain) thy xs asms prop tac;
   161 fun prove_plain thy xs asms prop tac = hd (prove_multi_plain thy xs asms [prop] tac);
   162 
   163 end;
   164 
   165 
   166 (* prove_raw -- no checks, no normalization of result! *)
   167 
   168 fun prove_raw casms cprop tac =
   169   (case SINGLE (tac (map (norm_hhf o Thm.assume) casms)) (init cprop) of
   170     SOME th => Drule.implies_intr_list casms (finish th)
   171   | NONE => raise ERROR_MESSAGE "Tactic failed.");
   172 
   173 
   174 (* SELECT_GOAL *)
   175 
   176 (*Tactical for restricting the effect of a tactic to subgoal i.  Works
   177   by making a new state from subgoal i, applying tac to it, and
   178   composing the resulting thm with the original state.*)
   179 
   180 fun SELECT tac i st =
   181   init (Thm.adjust_maxidx (List.nth (Drule.cprems_of st, i - 1)))
   182   |> tac
   183   |> Seq.maps (fn st' => Thm.bicompose false (false, conclude st', Thm.nprems_of st') i st);
   184 
   185 fun SELECT_GOAL tac i st =
   186   let val n = Thm.nprems_of st in
   187     if 1 <= i andalso i <= n then
   188       if n = 1 then tac st else SELECT tac i st
   189     else Seq.empty
   190   end;
   191 
   192 end;
   193 
   194 structure BasicGoal: BASIC_GOAL = Goal;
   195 open BasicGoal;