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