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