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