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