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