src/Pure/goal.ML
author haftmann
Fri Nov 10 07:44:47 2006 +0100 (2006-11-10)
changeset 21286 b5e7b80caa6a
parent 20290 3f886c176c9b
child 21516 c2a116a2c4fd
permissions -rw-r--r--
introduces canonical AList functions for loop_tacs
     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_raw: cterm list -> cterm -> (thm list -> tactic) -> thm
    24   val prove_multi: Proof.context -> string list -> term list -> term list ->
    25     ({prems: thm list, context: Proof.context} -> tactic) -> thm list
    26   val prove: Proof.context -> string list -> term list -> term ->
    27     ({prems: thm list, context: Proof.context} -> tactic) -> thm
    28   val prove_global: theory -> string list -> term list -> term -> (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 val init =
    43   let val A = #1 (Drule.dest_implies (Thm.cprop_of Drule.protectI))
    44   in fn C => Thm.instantiate ([], [(A, C)]) Drule.protectI end;
    45 
    46 (*
    47    C
    48   --- (protect)
    49   #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.compose_no_flatten 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 (* composition of normal results *)
    81 
    82 fun compose_hhf tha i thb =
    83   Thm.bicompose false (false, Drule.lift_all (Thm.cprem_of thb i) tha, 0) i thb;
    84 
    85 fun compose_hhf_tac th i = PRIMSEQ (compose_hhf th i);
    86 
    87 fun comp_hhf tha thb =
    88   (case Seq.chop 2 (compose_hhf tha 1 thb) of
    89     ([th], _) => th
    90   | ([], _) => raise THM ("comp_hhf: no unifiers", 1, [tha, thb])
    91   | _  => raise THM ("comp_hhf: multiple unifiers", 1, [tha, thb]));
    92 
    93 
    94 
    95 (** tactical theorem proving **)
    96 
    97 (* prove_raw -- no checks, no normalization of result! *)
    98 
    99 fun prove_raw casms cprop tac =
   100   (case SINGLE (tac (map Assumption.assume casms)) (init cprop) of
   101     SOME th => Drule.implies_intr_list casms (finish th)
   102   | NONE => error "Tactic failed.");
   103 
   104 
   105 (* prove_multi *)
   106 
   107 fun prove_multi ctxt xs asms props tac =
   108   let
   109     val thy = Context.theory_of_proof ctxt;
   110     val string_of_term = Sign.string_of_term thy;
   111 
   112     fun err msg = cat_error msg
   113       ("The error(s) above occurred for the goal statement:\n" ^
   114         string_of_term (Logic.list_implies (asms, Logic.mk_conjunction_list props)));
   115 
   116     fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm (Term.no_dummy_patterns t))
   117       handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
   118     val casms = map cert_safe asms;
   119     val cprops = map cert_safe props;
   120 
   121     val (prems, ctxt') = ctxt
   122       |> Variable.add_fixes_direct xs
   123       |> fold Variable.declare_internal (asms @ props)
   124       |> Assumption.add_assumes casms;
   125 
   126     val goal = init (Conjunction.mk_conjunction_list cprops);
   127     val res =
   128       (case SINGLE (tac {prems = prems, context = ctxt'}) goal of
   129         NONE => err "Tactic failed."
   130       | SOME res => res);
   131     val [results] = Conjunction.elim_precise [length props] (finish res)
   132       handle THM (msg, _, _) => err msg;
   133     val _ = Unify.matches_list thy (map Thm.term_of cprops) (map Thm.prop_of results)
   134       orelse err ("Proved a different theorem: " ^ string_of_term (Thm.prop_of res));
   135   in
   136     results
   137     |> map (Assumption.export false ctxt' ctxt)
   138     |> Variable.export ctxt' ctxt
   139     |> map Drule.zero_var_indexes
   140   end;
   141 
   142 
   143 (* prove *)
   144 
   145 fun prove ctxt xs asms prop tac = hd (prove_multi ctxt xs asms [prop] tac);
   146 
   147 fun prove_global thy xs asms prop tac =
   148   Drule.standard (prove (Context.init_proof thy) xs asms prop (fn {prems, ...} => tac prems));
   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_cterm ~1 #> 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;