src/Pure/goal.ML
author wenzelm
Thu Apr 27 15:06:35 2006 +0200 (2006-04-27)
changeset 19482 9f11af8f7ef9
parent 19423 51eeee99bd8f
child 19619 ee1104f972a4
permissions -rw-r--r--
tuned basic list operators (flat, maps, map_filter);
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@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@18207
    78
(* HHF normal form: !! before ==>, outermost !! generalized *)
wenzelm@18207
    79
wenzelm@18207
    80
local
wenzelm@17980
    81
wenzelm@18207
    82
fun gen_norm_hhf ss =
wenzelm@18207
    83
  (not o Drule.is_norm_hhf o Thm.prop_of) ?
wenzelm@18207
    84
    Drule.fconv_rule (MetaSimplifier.rewrite_cterm (true, false, false) (K (K NONE)) ss)
wenzelm@18207
    85
  #> Thm.adjust_maxidx_thm
wenzelm@18207
    86
  #> Drule.gen_all;
wenzelm@18207
    87
wenzelm@18207
    88
val ss =
wenzelm@18180
    89
  MetaSimplifier.theory_context ProtoPure.thy MetaSimplifier.empty_ss
wenzelm@18180
    90
    addsimps [Drule.norm_hhf_eq];
wenzelm@18180
    91
wenzelm@18207
    92
in
wenzelm@18180
    93
wenzelm@18207
    94
val norm_hhf = gen_norm_hhf ss;
wenzelm@18252
    95
val norm_hhf_protect = gen_norm_hhf (ss addeqcongs [Drule.protect_cong]);
wenzelm@17980
    96
wenzelm@18207
    97
end;
wenzelm@18180
    98
wenzelm@18027
    99
wenzelm@18027
   100
(* composition of normal results *)
wenzelm@18027
   101
wenzelm@18027
   102
fun compose_hhf tha i thb =
wenzelm@18145
   103
  Thm.bicompose false (false, Drule.lift_all (Thm.cprem_of thb i) tha, 0) i thb;
wenzelm@18027
   104
wenzelm@18119
   105
fun compose_hhf_tac th i = PRIMSEQ (compose_hhf th i);
wenzelm@18119
   106
wenzelm@18027
   107
fun comp_hhf tha thb =
wenzelm@18119
   108
  (case Seq.chop (2, compose_hhf tha 1 thb) of
wenzelm@18119
   109
    ([th], _) => th
wenzelm@18119
   110
  | ([], _) => raise THM ("comp_hhf: no unifiers", 1, [tha, thb])
wenzelm@18119
   111
  | _  => raise THM ("comp_hhf: multiple unifiers", 1, [tha, thb]));
wenzelm@17986
   112
wenzelm@17986
   113
wenzelm@18027
   114
wenzelm@18027
   115
(** tactical theorem proving **)
wenzelm@18027
   116
wenzelm@18119
   117
(* prove_multi *)
wenzelm@17986
   118
wenzelm@18119
   119
fun prove_multi thy xs asms props tac =
wenzelm@17980
   120
  let
wenzelm@17980
   121
    val prop = Logic.mk_conjunction_list props;
wenzelm@17980
   122
    val statement = Logic.list_implies (asms, prop);
wenzelm@18139
   123
    val frees = Term.add_frees statement [];
wenzelm@17980
   124
    val fixed_frees = filter_out (member (op =) xs o #1) frees;
wenzelm@18139
   125
    val fixed_tfrees = fold (Term.add_tfreesT o #2) fixed_frees [];
wenzelm@19482
   126
    val params = map_filter (fn x => Option.map (pair x) (AList.lookup (op =) frees x)) xs;
wenzelm@17980
   127
wenzelm@18678
   128
    fun err msg = cat_error msg
wenzelm@18678
   129
      ("The error(s) above occurred for the goal statement:\n" ^
wenzelm@17980
   130
        Sign.string_of_term thy (Term.list_all_free (params, statement)));
wenzelm@17980
   131
wenzelm@17980
   132
    fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm t)
wenzelm@17980
   133
      handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
wenzelm@17980
   134
wenzelm@17980
   135
    val _ = cert_safe statement;
wenzelm@17980
   136
    val _ = Term.no_dummy_patterns statement handle TERM (msg, _) => err msg;
wenzelm@17980
   137
wenzelm@17980
   138
    val cparams = map (cert_safe o Free) params;
wenzelm@17980
   139
    val casms = map cert_safe asms;
wenzelm@18027
   140
    val prems = map (norm_hhf o Thm.assume) casms;
wenzelm@17980
   141
wenzelm@17980
   142
    val goal = init (cert_safe prop);
wenzelm@18119
   143
    val goal' = case SINGLE (tac prems) goal of SOME goal' => goal' | _ => err "Tactic failed.";
wenzelm@17980
   144
    val raw_result = finish goal' handle THM (msg, _, _) => err msg;
wenzelm@17980
   145
wenzelm@17980
   146
    val prop' = Thm.prop_of raw_result;
wenzelm@17986
   147
    val _ = conditional (not (Pattern.matches thy (Envir.beta_norm prop, prop'))) (fn () =>
wenzelm@17980
   148
      err ("Proved a different theorem: " ^ Sign.string_of_term thy prop'));
wenzelm@17980
   149
  in
wenzelm@19423
   150
    hd (Conjunction.elim_precise [length props] raw_result)
wenzelm@17980
   151
    |> map
wenzelm@17980
   152
      (Drule.implies_intr_list casms
wenzelm@17980
   153
        #> Drule.forall_intr_list cparams
wenzelm@18119
   154
        #> norm_hhf
wenzelm@18139
   155
        #> Thm.varifyT' fixed_tfrees
wenzelm@18139
   156
        #-> K Drule.zero_var_indexes)
wenzelm@17980
   157
  end;
wenzelm@17980
   158
wenzelm@17980
   159
wenzelm@18119
   160
(* prove *)
wenzelm@17980
   161
wenzelm@17980
   162
fun prove thy xs asms prop tac = hd (prove_multi thy xs asms [prop] tac);
wenzelm@17980
   163
wenzelm@17980
   164
wenzelm@18027
   165
(* prove_raw -- no checks, no normalization of result! *)
wenzelm@18027
   166
wenzelm@18027
   167
fun prove_raw casms cprop tac =
wenzelm@18027
   168
  (case SINGLE (tac (map (norm_hhf o Thm.assume) casms)) (init cprop) of
wenzelm@18027
   169
    SOME th => Drule.implies_intr_list casms (finish th)
wenzelm@18678
   170
  | NONE => error "Tactic failed.");
wenzelm@18027
   171
wenzelm@18027
   172
wenzelm@17980
   173
wenzelm@19184
   174
(** local goal states **)
wenzelm@18207
   175
wenzelm@19184
   176
fun extract i n st =
wenzelm@19184
   177
  (if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st then Seq.empty
wenzelm@19184
   178
   else if n = 1 then Seq.single (Thm.cprem_of st i)
wenzelm@19423
   179
   else Seq.single (foldr1 Conjunction.mk_conjunction (map (Thm.cprem_of st) (i upto i + n - 1))))
wenzelm@19184
   180
  |> Seq.map (Thm.adjust_maxidx #> init);
wenzelm@17980
   181
wenzelm@19221
   182
fun retrofit i n st' st =
wenzelm@19221
   183
  (if n = 1 then st
wenzelm@19423
   184
   else st |> Drule.rotate_prems (i - 1) |> Conjunction.uncurry n |> Drule.rotate_prems (1 - i))
wenzelm@19221
   185
  |> Thm.compose_no_flatten false (conclude st', Thm.nprems_of st') i;
wenzelm@18207
   186
wenzelm@17980
   187
fun SELECT_GOAL tac i st =
wenzelm@19191
   188
  if Thm.nprems_of st = 1 andalso i = 1 then tac st
wenzelm@19184
   189
  else Seq.lifts (retrofit i 1) (Seq.maps tac (extract i 1 st)) st;
wenzelm@17980
   190
wenzelm@18207
   191
end;
wenzelm@18207
   192
wenzelm@17980
   193
structure BasicGoal: BASIC_GOAL = Goal;
wenzelm@17980
   194
open BasicGoal;