src/Pure/goal.ML
author wenzelm
Sat Jul 08 12:54:44 2006 +0200 (2006-07-08)
changeset 20056 0698a403a066
parent 19862 7f29aa958b72
child 20157 28638d2a6bc7
permissions -rw-r--r--
prove/prove_multi: context;
prove_global: theory + standard;
tuned;
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@20056
    25
  val prove_multi: Context.proof -> string list -> term list -> term list ->
wenzelm@17980
    26
    (thm list -> tactic) -> thm list
wenzelm@20056
    27
  val prove: Context.proof -> string list -> term list -> term -> (thm list -> tactic) -> thm
wenzelm@20056
    28
  val prove_global: theory -> string list -> term list -> term -> (thm list -> tactic) -> thm
wenzelm@18027
    29
  val prove_raw: cterm list -> cterm -> (thm list -> tactic) -> thm
wenzelm@19184
    30
  val extract: int -> int -> thm -> thm Seq.seq
wenzelm@19184
    31
  val retrofit: int -> int -> thm -> thm -> thm Seq.seq
wenzelm@17980
    32
end;
wenzelm@17980
    33
wenzelm@17980
    34
structure Goal: GOAL =
wenzelm@17980
    35
struct
wenzelm@17980
    36
wenzelm@18027
    37
(** goals **)
wenzelm@18027
    38
wenzelm@18027
    39
(*
wenzelm@18027
    40
  -------- (init)
wenzelm@18027
    41
  C ==> #C
wenzelm@18027
    42
*)
wenzelm@18027
    43
fun init ct = Drule.instantiate' [] [SOME ct] Drule.protectI;
wenzelm@17980
    44
wenzelm@17980
    45
(*
wenzelm@18119
    46
   C
wenzelm@18119
    47
  --- (protect)
wenzelm@18119
    48
  #C
wenzelm@17980
    49
*)
wenzelm@18027
    50
fun protect th = th COMP Drule.incr_indexes th Drule.protectI;
wenzelm@17980
    51
wenzelm@17980
    52
(*
wenzelm@18027
    53
  A ==> ... ==> #C
wenzelm@18027
    54
  ---------------- (conclude)
wenzelm@17980
    55
  A ==> ... ==> C
wenzelm@17980
    56
*)
wenzelm@17980
    57
fun conclude th =
wenzelm@18497
    58
  (case SINGLE (Thm.compose_no_flatten false (th, Thm.nprems_of th) 1)
wenzelm@18027
    59
      (Drule.incr_indexes th Drule.protectD) of
wenzelm@17980
    60
    SOME th' => th'
wenzelm@17980
    61
  | NONE => raise THM ("Failed to conclude goal", 0, [th]));
wenzelm@17980
    62
wenzelm@17980
    63
(*
wenzelm@18027
    64
  #C
wenzelm@18027
    65
  --- (finish)
wenzelm@18027
    66
   C
wenzelm@17983
    67
*)
wenzelm@17980
    68
fun finish th =
wenzelm@17980
    69
  (case Thm.nprems_of th of
wenzelm@17980
    70
    0 => conclude th
wenzelm@17980
    71
  | n => raise THM ("Proof failed.\n" ^
wenzelm@17980
    72
      Pretty.string_of (Pretty.chunks (Display.pretty_goals n th)) ^
wenzelm@17980
    73
      ("\n" ^ string_of_int n ^ " unsolved goal(s)!"), 0, [th]));
wenzelm@17980
    74
wenzelm@17980
    75
wenzelm@18027
    76
wenzelm@18027
    77
(** results **)
wenzelm@18027
    78
wenzelm@18207
    79
(* HHF normal form: !! before ==>, outermost !! generalized *)
wenzelm@18207
    80
wenzelm@18207
    81
local
wenzelm@17980
    82
wenzelm@18207
    83
fun gen_norm_hhf ss =
wenzelm@18207
    84
  (not o Drule.is_norm_hhf o Thm.prop_of) ?
wenzelm@18207
    85
    Drule.fconv_rule (MetaSimplifier.rewrite_cterm (true, false, false) (K (K NONE)) ss)
wenzelm@18207
    86
  #> Thm.adjust_maxidx_thm
wenzelm@18207
    87
  #> Drule.gen_all;
wenzelm@18207
    88
wenzelm@18207
    89
val ss =
wenzelm@18180
    90
  MetaSimplifier.theory_context ProtoPure.thy MetaSimplifier.empty_ss
wenzelm@18180
    91
    addsimps [Drule.norm_hhf_eq];
wenzelm@18180
    92
wenzelm@18207
    93
in
wenzelm@18180
    94
wenzelm@18207
    95
val norm_hhf = gen_norm_hhf ss;
wenzelm@18252
    96
val norm_hhf_protect = gen_norm_hhf (ss addeqcongs [Drule.protect_cong]);
wenzelm@17980
    97
wenzelm@18207
    98
end;
wenzelm@18180
    99
wenzelm@18027
   100
wenzelm@18027
   101
(* composition of normal results *)
wenzelm@18027
   102
wenzelm@18027
   103
fun compose_hhf tha i thb =
wenzelm@18145
   104
  Thm.bicompose false (false, Drule.lift_all (Thm.cprem_of thb i) tha, 0) i thb;
wenzelm@18027
   105
wenzelm@18119
   106
fun compose_hhf_tac th i = PRIMSEQ (compose_hhf th i);
wenzelm@18119
   107
wenzelm@18027
   108
fun comp_hhf tha thb =
wenzelm@19862
   109
  (case Seq.chop 2 (compose_hhf tha 1 thb) of
wenzelm@18119
   110
    ([th], _) => th
wenzelm@18119
   111
  | ([], _) => raise THM ("comp_hhf: no unifiers", 1, [tha, thb])
wenzelm@18119
   112
  | _  => raise THM ("comp_hhf: multiple unifiers", 1, [tha, thb]));
wenzelm@17986
   113
wenzelm@17986
   114
wenzelm@18027
   115
wenzelm@18027
   116
(** tactical theorem proving **)
wenzelm@18027
   117
wenzelm@18119
   118
(* prove_multi *)
wenzelm@17986
   119
wenzelm@20056
   120
fun prove_multi ctxt xs asms props tac =
wenzelm@17980
   121
  let
wenzelm@20056
   122
    val thy = Context.theory_of_proof ctxt;
wenzelm@20056
   123
    val string_of_term = Sign.string_of_term thy;
wenzelm@20056
   124
wenzelm@17980
   125
    val prop = Logic.mk_conjunction_list props;
wenzelm@17980
   126
    val statement = Logic.list_implies (asms, prop);
wenzelm@17980
   127
wenzelm@18678
   128
    fun err msg = cat_error msg
wenzelm@20056
   129
      ("The error(s) above occurred for the goal statement:\n" ^ string_of_term statement);
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
    val casms = map cert_safe asms;
wenzelm@18027
   136
    val prems = map (norm_hhf o Thm.assume) casms;
wenzelm@17980
   137
wenzelm@20056
   138
    val ctxt' = ctxt
wenzelm@20056
   139
      |> Variable.set_body false
wenzelm@20056
   140
      |> (snd o Variable.add_fixes xs)
wenzelm@20056
   141
      |> fold Variable.declare_term (asms @ props);
wenzelm@20056
   142
wenzelm@19774
   143
    val res =
wenzelm@19774
   144
      (case SINGLE (tac prems) (init (cert_safe prop)) of
wenzelm@19774
   145
        NONE => err "Tactic failed."
wenzelm@19774
   146
      | SOME res => res);
wenzelm@19774
   147
    val [results] =
wenzelm@19774
   148
      Conjunction.elim_precise [length props] (finish res) handle THM (msg, _, _) => err msg;
wenzelm@19862
   149
    val _ = Unify.matches_list thy (map (Thm.term_of o cert_safe) props) (map Thm.prop_of results)
wenzelm@20056
   150
      orelse err ("Proved a different theorem: " ^ string_of_term (Thm.prop_of res));
wenzelm@17980
   151
  in
wenzelm@20056
   152
    results
wenzelm@20056
   153
    |> map (Drule.implies_intr_list casms)
wenzelm@20056
   154
    |> Variable.export ctxt' ctxt
wenzelm@20056
   155
    |> map (norm_hhf #> Drule.zero_var_indexes)
wenzelm@17980
   156
  end;
wenzelm@17980
   157
wenzelm@17980
   158
wenzelm@18119
   159
(* prove *)
wenzelm@17980
   160
wenzelm@20056
   161
fun prove ctxt xs asms prop tac = hd (prove_multi ctxt xs asms [prop] tac);
wenzelm@20056
   162
wenzelm@20056
   163
fun prove_global thy xs asms prop tac =
wenzelm@20056
   164
  Drule.standard (prove (Context.init_proof thy) xs asms prop tac);
wenzelm@17980
   165
wenzelm@17980
   166
wenzelm@18027
   167
(* prove_raw -- no checks, no normalization of result! *)
wenzelm@18027
   168
wenzelm@18027
   169
fun prove_raw casms cprop tac =
wenzelm@18027
   170
  (case SINGLE (tac (map (norm_hhf o Thm.assume) casms)) (init cprop) of
wenzelm@18027
   171
    SOME th => Drule.implies_intr_list casms (finish th)
wenzelm@18678
   172
  | NONE => error "Tactic failed.");
wenzelm@18027
   173
wenzelm@18027
   174
wenzelm@17980
   175
wenzelm@19184
   176
(** local goal states **)
wenzelm@18207
   177
wenzelm@19184
   178
fun extract i n st =
wenzelm@19184
   179
  (if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st then Seq.empty
wenzelm@19184
   180
   else if n = 1 then Seq.single (Thm.cprem_of st i)
wenzelm@19423
   181
   else Seq.single (foldr1 Conjunction.mk_conjunction (map (Thm.cprem_of st) (i upto i + n - 1))))
wenzelm@19184
   182
  |> Seq.map (Thm.adjust_maxidx #> init);
wenzelm@17980
   183
wenzelm@19221
   184
fun retrofit i n st' st =
wenzelm@19221
   185
  (if n = 1 then st
wenzelm@19423
   186
   else st |> Drule.rotate_prems (i - 1) |> Conjunction.uncurry n |> Drule.rotate_prems (1 - i))
wenzelm@19221
   187
  |> Thm.compose_no_flatten false (conclude st', Thm.nprems_of st') i;
wenzelm@18207
   188
wenzelm@17980
   189
fun SELECT_GOAL tac i st =
wenzelm@19191
   190
  if Thm.nprems_of st = 1 andalso i = 1 then tac st
wenzelm@19184
   191
  else Seq.lifts (retrofit i 1) (Seq.maps tac (extract i 1 st)) st;
wenzelm@17980
   192
wenzelm@18207
   193
end;
wenzelm@18207
   194
wenzelm@17980
   195
structure BasicGoal: BASIC_GOAL = Goal;
wenzelm@17980
   196
open BasicGoal;