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