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