src/Pure/goal.ML
author wenzelm
Sat Jul 08 12:54:44 2006 +0200 (2006-07-08)
changeset 20056 0698a403a066
parent 19862 7f29aa958b72
child 20157 28638d2a6bc7
permissions -rw-r--r--
prove/prove_multi: context;
prove_global: theory + standard;
tuned;
     1 (*  Title:      Pure/goal.ML
     2     ID:         $Id$
     3     Author:     Makarius and Lawrence C Paulson
     4 
     5 Goals in tactical theorem proving.
     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 norm_hhf_protect: thm -> thm
    22   val compose_hhf: thm -> int -> thm -> thm Seq.seq
    23   val compose_hhf_tac: thm -> int -> tactic
    24   val comp_hhf: thm -> thm -> thm
    25   val prove_multi: Context.proof -> string list -> term list -> term list ->
    26     (thm list -> tactic) -> thm list
    27   val prove: Context.proof -> string list -> term list -> term -> (thm list -> tactic) -> thm
    28   val prove_global: theory -> string list -> term list -> term -> (thm list -> tactic) -> thm
    29   val prove_raw: cterm list -> cterm -> (thm list -> tactic) -> thm
    30   val extract: int -> int -> thm -> thm Seq.seq
    31   val retrofit: int -> int -> thm -> thm -> thm Seq.seq
    32 end;
    33 
    34 structure Goal: GOAL =
    35 struct
    36 
    37 (** goals **)
    38 
    39 (*
    40   -------- (init)
    41   C ==> #C
    42 *)
    43 fun init ct = Drule.instantiate' [] [SOME ct] Drule.protectI;
    44 
    45 (*
    46    C
    47   --- (protect)
    48   #C
    49 *)
    50 fun protect th = th COMP Drule.incr_indexes th Drule.protectI;
    51 
    52 (*
    53   A ==> ... ==> #C
    54   ---------------- (conclude)
    55   A ==> ... ==> C
    56 *)
    57 fun conclude th =
    58   (case SINGLE (Thm.compose_no_flatten false (th, Thm.nprems_of th) 1)
    59       (Drule.incr_indexes th Drule.protectD) of
    60     SOME th' => th'
    61   | NONE => raise THM ("Failed to conclude goal", 0, [th]));
    62 
    63 (*
    64   #C
    65   --- (finish)
    66    C
    67 *)
    68 fun finish th =
    69   (case Thm.nprems_of th of
    70     0 => conclude th
    71   | n => raise THM ("Proof failed.\n" ^
    72       Pretty.string_of (Pretty.chunks (Display.pretty_goals n th)) ^
    73       ("\n" ^ string_of_int n ^ " unsolved goal(s)!"), 0, [th]));
    74 
    75 
    76 
    77 (** results **)
    78 
    79 (* HHF normal form: !! before ==>, outermost !! generalized *)
    80 
    81 local
    82 
    83 fun gen_norm_hhf ss =
    84   (not o Drule.is_norm_hhf o Thm.prop_of) ?
    85     Drule.fconv_rule (MetaSimplifier.rewrite_cterm (true, false, false) (K (K NONE)) ss)
    86   #> Thm.adjust_maxidx_thm
    87   #> Drule.gen_all;
    88 
    89 val ss =
    90   MetaSimplifier.theory_context ProtoPure.thy MetaSimplifier.empty_ss
    91     addsimps [Drule.norm_hhf_eq];
    92 
    93 in
    94 
    95 val norm_hhf = gen_norm_hhf ss;
    96 val norm_hhf_protect = gen_norm_hhf (ss addeqcongs [Drule.protect_cong]);
    97 
    98 end;
    99 
   100 
   101 (* composition of normal results *)
   102 
   103 fun compose_hhf tha i thb =
   104   Thm.bicompose false (false, Drule.lift_all (Thm.cprem_of thb i) tha, 0) i thb;
   105 
   106 fun compose_hhf_tac th i = PRIMSEQ (compose_hhf th i);
   107 
   108 fun comp_hhf tha thb =
   109   (case Seq.chop 2 (compose_hhf tha 1 thb) of
   110     ([th], _) => th
   111   | ([], _) => raise THM ("comp_hhf: no unifiers", 1, [tha, thb])
   112   | _  => raise THM ("comp_hhf: multiple unifiers", 1, [tha, thb]));
   113 
   114 
   115 
   116 (** tactical theorem proving **)
   117 
   118 (* prove_multi *)
   119 
   120 fun prove_multi ctxt xs asms props tac =
   121   let
   122     val thy = Context.theory_of_proof ctxt;
   123     val string_of_term = Sign.string_of_term thy;
   124 
   125     val prop = Logic.mk_conjunction_list props;
   126     val statement = Logic.list_implies (asms, prop);
   127 
   128     fun err msg = cat_error msg
   129       ("The error(s) above occurred for the goal statement:\n" ^ string_of_term statement);
   130     fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm t)
   131       handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
   132 
   133     val _ = cert_safe statement;
   134     val _ = Term.no_dummy_patterns statement handle TERM (msg, _) => err msg;
   135     val casms = map cert_safe asms;
   136     val prems = map (norm_hhf o Thm.assume) casms;
   137 
   138     val ctxt' = ctxt
   139       |> Variable.set_body false
   140       |> (snd o Variable.add_fixes xs)
   141       |> fold Variable.declare_term (asms @ props);
   142 
   143     val res =
   144       (case SINGLE (tac prems) (init (cert_safe prop)) of
   145         NONE => err "Tactic failed."
   146       | SOME res => res);
   147     val [results] =
   148       Conjunction.elim_precise [length props] (finish res) handle THM (msg, _, _) => err msg;
   149     val _ = Unify.matches_list thy (map (Thm.term_of o cert_safe) props) (map Thm.prop_of results)
   150       orelse err ("Proved a different theorem: " ^ string_of_term (Thm.prop_of res));
   151   in
   152     results
   153     |> map (Drule.implies_intr_list casms)
   154     |> Variable.export ctxt' ctxt
   155     |> map (norm_hhf #> Drule.zero_var_indexes)
   156   end;
   157 
   158 
   159 (* prove *)
   160 
   161 fun prove ctxt xs asms prop tac = hd (prove_multi ctxt xs asms [prop] tac);
   162 
   163 fun prove_global thy xs asms prop tac =
   164   Drule.standard (prove (Context.init_proof thy) xs asms prop tac);
   165 
   166 
   167 (* prove_raw -- no checks, no normalization of result! *)
   168 
   169 fun prove_raw casms cprop tac =
   170   (case SINGLE (tac (map (norm_hhf o Thm.assume) casms)) (init cprop) of
   171     SOME th => Drule.implies_intr_list casms (finish th)
   172   | NONE => error "Tactic failed.");
   173 
   174 
   175 
   176 (** local goal states **)
   177 
   178 fun extract i n st =
   179   (if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st then Seq.empty
   180    else if n = 1 then Seq.single (Thm.cprem_of st i)
   181    else Seq.single (foldr1 Conjunction.mk_conjunction (map (Thm.cprem_of st) (i upto i + n - 1))))
   182   |> Seq.map (Thm.adjust_maxidx #> init);
   183 
   184 fun retrofit i n st' st =
   185   (if n = 1 then st
   186    else st |> Drule.rotate_prems (i - 1) |> Conjunction.uncurry n |> Drule.rotate_prems (1 - i))
   187   |> Thm.compose_no_flatten false (conclude st', Thm.nprems_of st') i;
   188 
   189 fun SELECT_GOAL tac i st =
   190   if Thm.nprems_of st = 1 andalso i = 1 then tac st
   191   else Seq.lifts (retrofit i 1) (Seq.maps tac (extract i 1 st)) st;
   192 
   193 end;
   194 
   195 structure BasicGoal: BASIC_GOAL = Goal;
   196 open BasicGoal;