src/Pure/goal.ML
author wenzelm
Fri, 21 Oct 2005 18:20:29 +0200
changeset 17983 89103008502f
parent 17980 788836292b1a
child 17986 0450847646c3
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17980
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/goal.ML
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius and Lawrence C Paulson
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
     4
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
     5
Internal goals.  NB: by attaching the Goal constant the conclusion of
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
     6
a goal state is guaranteed to be atomic.
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
     7
*)
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
     8
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
     9
signature BASIC_GOAL =
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    10
sig
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    11
  val SELECT_GOAL: tactic -> int -> tactic
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    12
end;
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    13
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    14
signature GOAL =
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    15
sig
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    16
  include BASIC_GOAL
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    17
  val init: cterm -> thm
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    18
  val conclude: thm -> thm
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    19
  val finish: thm -> thm
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    20
  val prove_raw: theory -> term -> tactic -> thm
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    21
  val norm_hhf_rule: thm -> thm
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    22
  val prove: theory -> string list -> term list -> term -> (thm list -> tactic) -> thm
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    23
  val prove_multi: theory -> string list -> term list -> term list ->
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    24
    (thm list -> tactic) -> thm list
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    25
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    26
  (* FIXME remove *)
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    27
  val norm_hhf_plain: thm -> thm
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    28
  val prove_multi_plain: theory -> string list -> term list -> term list ->
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    29
    (thm list -> tactic) -> thm list
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    30
  val prove_plain: theory -> string list -> term list -> term -> (thm list -> tactic) -> thm
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    31
end;
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    32
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    33
structure Goal: GOAL =
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    34
struct
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    35
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    36
(* managing goal states *)
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    37
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    38
(*
17983
wenzelm
parents: 17980
diff changeset
    39
  ------------ (init)
wenzelm
parents: 17980
diff changeset
    40
  C ==> Goal C
17980
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    41
*)
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    42
fun init ct = Drule.instantiate' [] [SOME ct] Drule.goalI;
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    43
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    44
(*
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    45
  A ==> ... ==> Goal C
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    46
  -------------------- (conclude)
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    47
  A ==> ... ==> C
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    48
*)
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    49
fun conclude th =
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    50
  (case SINGLE (Thm.bicompose false (false, th, Thm.nprems_of th) 1)
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    51
      (Drule.incr_indexes_wrt [] [] [] [th] Drule.goalD) of
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    52
    SOME th' => th'
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    53
  | NONE => raise THM ("Failed to conclude goal", 0, [th]));
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    54
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    55
(*
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    56
  Goal C
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    57
  ------ (finish)
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    58
    C
17983
wenzelm
parents: 17980
diff changeset
    59
*)
17980
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    60
fun finish th =
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    61
  (case Thm.nprems_of th of
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    62
    0 => conclude th
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    63
  | n => raise THM ("Proof failed.\n" ^
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    64
      Pretty.string_of (Pretty.chunks (Display.pretty_goals n th)) ^
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    65
      ("\n" ^ string_of_int n ^ " unsolved goal(s)!"), 0, [th]));
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    66
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    67
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    68
(* prove_raw -- minimal checks, no normalization *)
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    69
17983
wenzelm
parents: 17980
diff changeset
    70
fun prove_raw thy goal tac =
17980
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    71
  (case SINGLE tac (init (Thm.cterm_of thy goal)) of
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    72
    SOME th => finish th
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    73
  | NONE => raise ERROR_MESSAGE "Tactic failed.");
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    74
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    75
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    76
(* tactical proving *)
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    77
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    78
val norm_hhf_plain =  (* FIXME remove *)
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    79
  (not o Drule.is_norm_hhf o Thm.prop_of) ?
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    80
    MetaSimplifier.simplify_aux (K (K NONE)) true [Drule.norm_hhf_eq];
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    81
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    82
val norm_hhf_rule =
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    83
  norm_hhf_plain
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    84
  #> Thm.adjust_maxidx_thm
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    85
  #> Drule.gen_all;
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    86
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    87
local
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    88
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    89
fun gen_prove finish_thm thy xs asms props tac =
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    90
  let
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    91
    val prop = Logic.mk_conjunction_list props;
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    92
    val statement = Logic.list_implies (asms, prop);
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    93
    val frees = map Term.dest_Free (Term.term_frees statement);
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    94
    val fixed_frees = filter_out (member (op =) xs o #1) frees;
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    95
    val fixed_tfrees = foldr Term.add_typ_tfrees [] (map #2 fixed_frees);
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    96
    val params = List.mapPartial (fn x => Option.map (pair x) (AList.lookup (op =) frees x)) xs;
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    97
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    98
    fun err msg = raise ERROR_MESSAGE
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
    99
      (msg ^ "\nThe error(s) above occurred for the goal statement:\n" ^
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   100
        Sign.string_of_term thy (Term.list_all_free (params, statement)));
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   101
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   102
    fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm t)
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   103
      handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   104
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   105
    val _ = cert_safe statement;
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   106
    val _ = Term.no_dummy_patterns statement handle TERM (msg, _) => err msg;
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   107
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   108
    val cparams = map (cert_safe o Free) params;
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   109
    val casms = map cert_safe asms;
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   110
    val prems = map (norm_hhf_rule o Thm.assume) casms;
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   111
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   112
    val goal = init (cert_safe prop);
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   113
    val goal' = (case SINGLE (tac prems) goal of SOME goal' => goal' | _ => err "Tactic failed.");
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   114
    val raw_result = finish goal' handle THM (msg, _, _) => err msg;
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   115
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   116
    val prop' = Thm.prop_of raw_result;
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   117
    val _ = conditional (not (Pattern.matches thy (prop, prop'))) (fn () =>
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   118
      err ("Proved a different theorem: " ^ Sign.string_of_term thy prop'));
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   119
  in
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   120
    Drule.conj_elim_precise (length props) raw_result
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   121
    |> map
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   122
      (Drule.implies_intr_list casms
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   123
        #> Drule.forall_intr_list cparams
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   124
        #> finish_thm fixed_tfrees)
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   125
  end;
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   126
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   127
in
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   128
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   129
fun prove_multi thy xs asms prop tac =
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   130
  gen_prove (fn fixed_tfrees => Drule.zero_var_indexes o
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   131
      (#1 o Thm.varifyT' fixed_tfrees) o norm_hhf_rule)
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   132
    thy xs asms prop tac;
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   133
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   134
fun prove thy xs asms prop tac = hd (prove_multi thy xs asms [prop] tac);
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   135
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   136
fun prove_multi_plain thy xs asms prop tac = gen_prove (K norm_hhf_plain) thy xs asms prop tac;
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   137
fun prove_plain thy xs asms prop tac = hd (prove_multi_plain thy xs asms [prop] tac);
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   138
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   139
end;
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   140
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   141
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   142
(* SELECT_GOAL *)
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   143
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   144
(*Tactical for restricting the effect of a tactic to subgoal i.  Works
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   145
  by making a new state from subgoal i, applying tac to it, and
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   146
  composing the resulting thm with the original state.*)
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   147
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   148
fun SELECT tac i st =
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   149
  init (Thm.adjust_maxidx (List.nth (Drule.cprems_of st, i - 1)))
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   150
  |> tac
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   151
  |> Seq.maps (fn st' => Thm.bicompose false (false, conclude st', Thm.nprems_of st') i st);
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   152
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   153
fun SELECT_GOAL tac i st =
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   154
  let val n = Thm.nprems_of st in
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   155
    if 1 <= i andalso i <= n then
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   156
      if n = 1 then tac st else SELECT tac i st
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   157
    else Seq.empty
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   158
  end;
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   159
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   160
end;
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   161
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   162
structure BasicGoal: BASIC_GOAL = Goal;
788836292b1a Internal goals.
wenzelm
parents:
diff changeset
   163
open BasicGoal;