TFL/rules.ML
author paulson
Mon, 06 Aug 2001 12:42:43 +0200
changeset 11461 ffeac9aa1967
parent 10918 9679326489cd
child 11632 6fc8de600f58
permissions -rw-r--r--
removed an unsuitable default simprule
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10781
eedf2def44c1 tuned comment;
wenzelm
parents: 10769
diff changeset
     1
(*  Title:      TFL/rules.ML
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     3
    Author:     Konrad Slind, Cambridge University Computer Laboratory
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     4
    Copyright   1997  University of Cambridge
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     5
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     6
Emulation of HOL inference rules for TFL
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     7
*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     8
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     9
signature RULES =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    10
sig
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    11
  val dest_thm : thm -> term list * term
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    12
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    13
  (* Inference rules *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    14
  val REFL      :cterm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    15
  val ASSUME    :cterm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    16
  val MP        :thm -> thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    17
  val MATCH_MP  :thm -> thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    18
  val CONJUNCT1 :thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    19
  val CONJUNCT2 :thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    20
  val CONJUNCTS :thm -> thm list
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    21
  val DISCH     :cterm -> thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    22
  val UNDISCH   :thm  -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    23
  val SPEC      :cterm -> thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    24
  val ISPEC     :cterm -> thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    25
  val ISPECL    :cterm list -> thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    26
  val GEN       :cterm -> thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    27
  val GENL      :cterm list -> thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    28
  val LIST_CONJ :thm list -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    29
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    30
  val SYM : thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    31
  val DISCH_ALL : thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    32
  val FILTER_DISCH_ALL : (term -> bool) -> thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    33
  val SPEC_ALL  : thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    34
  val GEN_ALL   : thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    35
  val IMP_TRANS : thm -> thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    36
  val PROVE_HYP : thm -> thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    37
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    38
  val CHOOSE : cterm * thm -> thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    39
  val EXISTS : cterm * cterm -> thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    40
  val EXISTL : cterm list -> thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    41
  val IT_EXISTS : (cterm*cterm) list -> thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    42
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    43
  val EVEN_ORS : thm list -> thm list
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    44
  val DISJ_CASESL : thm -> thm list -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    45
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    46
  val list_beta_conv : cterm -> cterm list -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    47
  val SUBS : thm list -> thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    48
  val simpl_conv : simpset -> thm list -> cterm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    49
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    50
  val rbeta : thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    51
(* For debugging my isabelle solver in the conditional rewriter *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    52
  val term_ref : term list ref
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    53
  val thm_ref : thm list ref
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    54
  val mss_ref : meta_simpset list ref
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    55
  val tracing : bool ref
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    56
  val CONTEXT_REWRITE_RULE : term * term list * thm * thm list
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    57
                             -> thm -> thm * term list
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    58
  val RIGHT_ASSOC : thm -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    59
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    60
  val prove : cterm * tactic -> thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    61
end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    62
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    63
structure Rules: RULES =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    64
struct
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    65
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    66
structure S = USyntax;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    67
structure U = Utils;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    68
structure D = Dcterm;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    69
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    70
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    71
fun RULES_ERR func mesg = U.ERR {module = "Rules", func = func, mesg = mesg};
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    72
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    73
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    74
fun cconcl thm = D.drop_prop (#prop (Thm.crep_thm thm));
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    75
fun chyps thm = map D.drop_prop (#hyps (Thm.crep_thm thm));
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    76
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    77
fun dest_thm thm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    78
  let val {prop,hyps,...} = Thm.rep_thm thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    79
  in (map HOLogic.dest_Trueprop hyps, HOLogic.dest_Trueprop prop) end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    80
  handle TERM _ => raise RULES_ERR "dest_thm" "missing Trueprop";
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    81
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    82
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    83
(* Inference rules *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    84
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    85
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    86
 *        Equality (one step)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    87
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    88
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    89
fun REFL tm = Thm.reflexive tm RS meta_eq_to_obj_eq
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    90
  handle THM (msg, _, _) => raise RULES_ERR "REFL" msg;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    91
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    92
fun SYM thm = thm RS sym
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    93
  handle THM (msg, _, _) => raise RULES_ERR "SYM" msg;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    94
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    95
fun ALPHA thm ctm1 =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    96
  let
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    97
    val ctm2 = Thm.cprop_of thm;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    98
    val ctm2_eq = Thm.reflexive ctm2;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    99
    val ctm1_eq = Thm.reflexive ctm1;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   100
  in Thm.equal_elim (Thm.transitive ctm2_eq ctm1_eq) thm end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   101
  handle THM (msg, _, _) => raise RULES_ERR "ALPHA" msg;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   102
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   103
fun rbeta th =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   104
  (case D.strip_comb (cconcl th) of
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   105
    (_, [l, r]) => Thm.transitive th (Thm.beta_conversion false r)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   106
  | _ => raise RULES_ERR "rbeta" "");
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   107
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   108
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   109
(*----------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   110
 *        Implication and the assumption list
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   111
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   112
 * Assumptions get stuck on the meta-language assumption list. Implications
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   113
 * are in the object language, so discharging an assumption "A" from theorem
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   114
 * "B" results in something that looks like "A --> B".
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   115
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   116
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   117
fun ASSUME ctm = Thm.assume (D.mk_prop ctm);
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   118
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   119
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   120
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   121
 * Implication in TFL is -->. Meta-language implication (==>) is only used
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   122
 * in the implementation of some of the inference rules below.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   123
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   124
fun MP th1 th2 = th2 RS (th1 RS mp)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   125
  handle THM (msg, _, _) => raise RULES_ERR "MP" msg;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   126
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   127
(*forces the first argument to be a proposition if necessary*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   128
fun DISCH tm thm = Thm.implies_intr (D.mk_prop tm) thm COMP impI
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   129
  handle THM (msg, _, _) => raise RULES_ERR "DISCH" msg;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   130
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   131
fun DISCH_ALL thm = U.itlist DISCH (#hyps (Thm.crep_thm thm)) thm;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   132
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   133
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   134
fun FILTER_DISCH_ALL P thm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   135
 let fun check tm = P (#t (Thm.rep_cterm tm))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   136
 in  foldr (fn (tm,th) => if check tm then DISCH tm th else th)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   137
              (chyps thm, thm)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   138
 end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   139
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   140
(* freezeT expensive! *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   141
fun UNDISCH thm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   142
   let val tm = D.mk_prop (#1 (D.dest_imp (cconcl (Thm.freezeT thm))))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   143
   in Thm.implies_elim (thm RS mp) (ASSUME tm) end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   144
   handle U.ERR _ => raise RULES_ERR "UNDISCH" ""
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   145
     | THM _ => raise RULES_ERR "UNDISCH" "";
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   146
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   147
fun PROVE_HYP ath bth = MP (DISCH (cconcl ath) bth) ath;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   148
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   149
fun IMP_TRANS th1 th2 = th2 RS (th1 RS Thms.imp_trans)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   150
  handle THM (msg, _, _) => raise RULES_ERR "IMP_TRANS" msg;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   151
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   152
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   153
(*----------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   154
 *        Conjunction
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   155
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   156
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   157
fun CONJUNCT1 thm = thm RS conjunct1
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   158
  handle THM (msg, _, _) => raise RULES_ERR "CONJUNCT1" msg;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   159
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   160
fun CONJUNCT2 thm = thm RS conjunct2
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   161
  handle THM (msg, _, _) => raise RULES_ERR "CONJUNCT2" msg;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   162
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   163
fun CONJUNCTS th = CONJUNCTS (CONJUNCT1 th) @ CONJUNCTS (CONJUNCT2 th) handle U.ERR _ => [th];
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   164
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   165
fun LIST_CONJ [] = raise RULES_ERR "LIST_CONJ" "empty list"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   166
  | LIST_CONJ [th] = th
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   167
  | LIST_CONJ (th :: rst) = MP (MP (conjI COMP (impI RS impI)) th) (LIST_CONJ rst)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   168
      handle THM (msg, _, _) => raise RULES_ERR "LIST_CONJ" msg;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   169
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   170
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   171
(*----------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   172
 *        Disjunction
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   173
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   174
local val {prop,sign,...} = rep_thm disjI1
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   175
      val [P,Q] = term_vars prop
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   176
      val disj1 = Thm.forall_intr (Thm.cterm_of sign Q) disjI1
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   177
in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   178
fun DISJ1 thm tm = thm RS (forall_elim (D.drop_prop tm) disj1)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   179
  handle THM (msg, _, _) => raise RULES_ERR "DISJ1" msg;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   180
end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   181
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   182
local val {prop,sign,...} = rep_thm disjI2
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   183
      val [P,Q] = term_vars prop
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   184
      val disj2 = Thm.forall_intr (Thm.cterm_of sign P) disjI2
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   185
in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   186
fun DISJ2 tm thm = thm RS (forall_elim (D.drop_prop tm) disj2)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   187
  handle THM (msg, _, _) => raise RULES_ERR "DISJ2" msg;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   188
end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   189
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   190
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   191
(*----------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   192
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   193
 *                   A1 |- M1, ..., An |- Mn
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   194
 *     ---------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   195
 *     [A1 |- M1 \/ ... \/ Mn, ..., An |- M1 \/ ... \/ Mn]
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   196
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   197
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   198
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   199
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   200
fun EVEN_ORS thms =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   201
  let fun blue ldisjs [] _ = []
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   202
        | blue ldisjs (th::rst) rdisjs =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   203
            let val tail = tl rdisjs
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   204
                val rdisj_tl = D.list_mk_disj tail
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   205
            in U.itlist DISJ2 ldisjs (DISJ1 th rdisj_tl)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   206
               :: blue (ldisjs @ [cconcl th]) rst tail
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   207
            end handle U.ERR _ => [U.itlist DISJ2 ldisjs th]
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   208
   in blue [] thms (map cconcl thms) end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   209
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   210
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   211
(*----------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   212
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   213
 *         A |- P \/ Q   B,P |- R    C,Q |- R
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   214
 *     ---------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   215
 *                     A U B U C |- R
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   216
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   217
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   218
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   219
fun DISJ_CASES th1 th2 th3 =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   220
  let
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   221
    val c = D.drop_prop (cconcl th1);
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   222
    val (disj1, disj2) = D.dest_disj c;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   223
    val th2' = DISCH disj1 th2;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   224
    val th3' = DISCH disj2 th3;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   225
  in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   226
    th3' RS (th2' RS (th1 RS Thms.tfl_disjE))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   227
      handle THM (msg, _, _) => raise RULES_ERR "DISJ_CASES" msg
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   228
  end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   229
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   230
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   231
(*-----------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   232
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   233
 *       |- A1 \/ ... \/ An     [A1 |- M, ..., An |- M]
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   234
 *     ---------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   235
 *                           |- M
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   236
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   237
 * Note. The list of theorems may be all jumbled up, so we have to
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   238
 * first organize it to align with the first argument (the disjunctive
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   239
 * theorem).
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   240
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   241
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   242
fun organize eq =    (* a bit slow - analogous to insertion sort *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   243
 let fun extract a alist =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   244
     let fun ex (_,[]) = raise RULES_ERR "organize" "not a permutation.1"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   245
           | ex(left,h::t) = if (eq h a) then (h,rev left@t) else ex(h::left,t)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   246
     in ex ([],alist)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   247
     end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   248
     fun place [] [] = []
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   249
       | place (a::rst) alist =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   250
           let val (item,next) = extract a alist
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   251
           in item::place rst next
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   252
           end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   253
       | place _ _ = raise RULES_ERR "organize" "not a permutation.2"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   254
 in place
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   255
 end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   256
(* freezeT expensive! *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   257
fun DISJ_CASESL disjth thl =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   258
   let val c = cconcl disjth
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   259
       fun eq th atm = exists (fn t => HOLogic.dest_Trueprop t
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   260
                                       aconv term_of atm)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   261
                              (#hyps(rep_thm th))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   262
       val tml = D.strip_disj c
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   263
       fun DL th [] = raise RULES_ERR "DISJ_CASESL" "no cases"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   264
         | DL th [th1] = PROVE_HYP th th1
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   265
         | DL th [th1,th2] = DISJ_CASES th th1 th2
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   266
         | DL th (th1::rst) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   267
            let val tm = #2(D.dest_disj(D.drop_prop(cconcl th)))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   268
             in DISJ_CASES th th1 (DL (ASSUME tm) rst) end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   269
   in DL (freezeT disjth) (organize eq tml thl)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   270
   end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   271
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   272
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   273
(*----------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   274
 *        Universals
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   275
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   276
local (* this is fragile *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   277
      val {prop,sign,...} = rep_thm spec
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   278
      val x = hd (tl (term_vars prop))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   279
      val (TVar (indx,_)) = type_of x
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   280
      val gspec = forall_intr (cterm_of sign x) spec
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   281
in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   282
fun SPEC tm thm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   283
   let val {sign,T,...} = rep_cterm tm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   284
       val gspec' = instantiate([(indx,ctyp_of sign T)],[]) gspec
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   285
   in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   286
      thm RS (forall_elim tm gspec')
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   287
   end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   288
end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   289
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   290
fun SPEC_ALL thm = U.rev_itlist SPEC (#1(D.strip_forall(cconcl thm))) thm;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   291
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   292
val ISPEC = SPEC
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   293
val ISPECL = U.rev_itlist ISPEC;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   294
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   295
(* Not optimized! Too complicated. *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   296
local val {prop,sign,...} = rep_thm allI
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   297
      val [P] = add_term_vars (prop, [])
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   298
      fun cty_theta s = map (fn (i,ty) => (i, ctyp_of s ty))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   299
      fun ctm_theta s = map (fn (i,tm2) =>
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   300
                             let val ctm2 = cterm_of s tm2
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   301
                             in (cterm_of s (Var(i,#T(rep_cterm ctm2))), ctm2)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   302
                             end)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   303
      fun certify s (ty_theta,tm_theta) = (cty_theta s ty_theta,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   304
                                           ctm_theta s tm_theta)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   305
in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   306
fun GEN v th =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   307
   let val gth = forall_intr v th
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   308
       val {prop=Const("all",_)$Abs(x,ty,rst),sign,...} = rep_thm gth
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   309
       val P' = Abs(x,ty, HOLogic.dest_Trueprop rst)  (* get rid of trueprop *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   310
       val tsig = #tsig(Sign.rep_sg sign)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   311
       val theta = Pattern.match tsig (P,P')
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   312
       val allI2 = instantiate (certify sign theta) allI
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   313
       val thm = Thm.implies_elim allI2 gth
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   314
       val {prop = tp $ (A $ Abs(_,_,M)),sign,...} = rep_thm thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   315
       val prop' = tp $ (A $ Abs(x,ty,M))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   316
   in ALPHA thm (cterm_of sign prop')
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   317
   end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   318
end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   319
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   320
val GENL = U.itlist GEN;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   321
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   322
fun GEN_ALL thm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   323
   let val {prop,sign,...} = rep_thm thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   324
       val tycheck = cterm_of sign
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   325
       val vlist = map tycheck (add_term_vars (prop, []))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   326
  in GENL vlist thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   327
  end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   328
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   329
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   330
fun MATCH_MP th1 th2 =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   331
   if (D.is_forall (D.drop_prop(cconcl th1)))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   332
   then MATCH_MP (th1 RS spec) th2
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   333
   else MP th1 th2;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   334
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   335
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   336
(*----------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   337
 *        Existentials
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   338
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   339
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   340
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   341
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   342
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   343
 * Existential elimination
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   344
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   345
 *      A1 |- ?x.t[x]   ,   A2, "t[v]" |- t'
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   346
 *      ------------------------------------     (variable v occurs nowhere)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   347
 *                A1 u A2 |- t'
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   348
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   349
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   350
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   351
fun CHOOSE (fvar, exth) fact =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   352
  let
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   353
    val lam = #2 (D.dest_comb (D.drop_prop (cconcl exth)))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   354
    val redex = D.capply lam fvar
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   355
    val {sign, t = t$u,...} = Thm.rep_cterm redex
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   356
    val residue = Thm.cterm_of sign (betapply (t, u))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   357
  in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   358
    GEN fvar (DISCH residue fact) RS (exth RS Thms.choose_thm)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   359
      handle THM (msg, _, _) => raise RULES_ERR "CHOOSE" msg
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   360
  end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   361
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   362
local val {prop,sign,...} = rep_thm exI
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   363
      val [P,x] = term_vars prop
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   364
in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   365
fun EXISTS (template,witness) thm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   366
   let val {prop,sign,...} = rep_thm thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   367
       val P' = cterm_of sign P
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   368
       val x' = cterm_of sign x
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   369
       val abstr = #2 (D.dest_comb template)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   370
   in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   371
   thm RS (cterm_instantiate[(P',abstr), (x',witness)] exI)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   372
     handle THM (msg, _, _) => raise RULES_ERR "EXISTS" msg
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   373
   end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   374
end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   375
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   376
(*----------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   377
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   378
 *         A |- M
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   379
 *   -------------------   [v_1,...,v_n]
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   380
 *    A |- ?v1...v_n. M
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   381
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   382
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   383
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   384
fun EXISTL vlist th =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   385
  U.itlist (fn v => fn thm => EXISTS(D.mk_exists(v,cconcl thm), v) thm)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   386
           vlist th;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   387
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   388
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   389
(*----------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   390
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   391
 *       A |- M[x_1,...,x_n]
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   392
 *   ----------------------------   [(x |-> y)_1,...,(x |-> y)_n]
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   393
 *       A |- ?y_1...y_n. M
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   394
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   395
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   396
(* Could be improved, but needs "subst_free" for certified terms *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   397
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   398
fun IT_EXISTS blist th =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   399
   let val {sign,...} = rep_thm th
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   400
       val tych = cterm_of sign
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   401
       val detype = #t o rep_cterm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   402
       val blist' = map (fn (x,y) => (detype x, detype y)) blist
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   403
       fun ?v M  = cterm_of sign (S.mk_exists{Bvar=v,Body = M})
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   404
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   405
  in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   406
  U.itlist (fn (b as (r1,r2)) => fn thm =>
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   407
        EXISTS(?r2(subst_free[b]
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   408
                   (HOLogic.dest_Trueprop(#prop(rep_thm thm)))), tych r1)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   409
              thm)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   410
       blist' th
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   411
  end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   412
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   413
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   414
 *  Faster version, that fails for some as yet unknown reason
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   415
 * fun IT_EXISTS blist th =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   416
 *    let val {sign,...} = rep_thm th
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   417
 *        val tych = cterm_of sign
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   418
 *        fun detype (x,y) = ((#t o rep_cterm) x, (#t o rep_cterm) y)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   419
 *   in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   420
 *  fold (fn (b as (r1,r2), thm) =>
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   421
 *  EXISTS(D.mk_exists(r2, tych(subst_free[detype b](#t(rep_cterm(cconcl thm))))),
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   422
 *           r1) thm)  blist th
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   423
 *   end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   424
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   425
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   426
(*----------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   427
 *        Rewriting
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   428
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   429
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   430
fun SUBS thl =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   431
  rewrite_rule (map (fn th => th RS eq_reflection handle THM _ => th) thl);
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   432
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   433
local fun rew_conv mss = MetaSimplifier.rewrite_cterm (true,false,false) (K(K None)) mss
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   434
in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   435
fun simpl_conv ss thl ctm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   436
 rew_conv (MetaSimplifier.mss_of (#simps (MetaSimplifier.dest_mss (#mss (rep_ss ss))) @ thl)) ctm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   437
 RS meta_eq_to_obj_eq
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   438
end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   439
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   440
val RIGHT_ASSOC = rewrite_rule [Thms.disj_assoc];
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   441
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   442
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   443
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   444
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   445
 *                  TERMINATION CONDITION EXTRACTION
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   446
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   447
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   448
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   449
(* Object language quantifier, i.e., "!" *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   450
fun Forall v M = S.mk_forall{Bvar=v, Body=M};
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   451
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   452
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   453
(* Fragile: it's a cong if it is not "R y x ==> cut f R x y = f y" *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   454
fun is_cong thm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   455
  let val {prop, ...} = rep_thm thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   456
  in case prop
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   457
     of (Const("==>",_)$(Const("Trueprop",_)$ _) $
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   458
         (Const("==",_) $ (Const ("Wellfounded_Recursion.cut",_) $ f $ R $ a $ x) $ _)) => false
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   459
      | _ => true
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   460
  end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   461
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   462
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   463
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   464
fun dest_equal(Const ("==",_) $
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   465
               (Const ("Trueprop",_) $ lhs)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   466
               $ (Const ("Trueprop",_) $ rhs)) = {lhs=lhs, rhs=rhs}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   467
  | dest_equal(Const ("==",_) $ lhs $ rhs)  = {lhs=lhs, rhs=rhs}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   468
  | dest_equal tm = S.dest_eq tm;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   469
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   470
fun get_lhs tm = #lhs(dest_equal (HOLogic.dest_Trueprop tm));
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   471
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   472
fun dest_all used (Const("all",_) $ (a as Abs _)) = S.dest_abs used a
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   473
  | dest_all _ _ = raise RULES_ERR "dest_all" "not a !!";
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   474
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   475
val is_all = can (dest_all []);
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   476
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   477
fun strip_all used fm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   478
   if (is_all fm)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   479
   then let val ({Bvar, Body}, used') = dest_all used fm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   480
            val (bvs, core, used'') = strip_all used' Body
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   481
        in ((Bvar::bvs), core, used'')
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   482
        end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   483
   else ([], fm, used);
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   484
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   485
fun break_all(Const("all",_) $ Abs (_,_,body)) = body
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   486
  | break_all _ = raise RULES_ERR "break_all" "not a !!";
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   487
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   488
fun list_break_all(Const("all",_) $ Abs (s,ty,body)) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   489
     let val (L,core) = list_break_all body
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   490
     in ((s,ty)::L, core)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   491
     end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   492
  | list_break_all tm = ([],tm);
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   493
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   494
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   495
 * Rename a term of the form
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   496
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   497
 *      !!x1 ...xn. x1=M1 ==> ... ==> xn=Mn
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   498
 *                  ==> ((%v1...vn. Q) x1 ... xn = g x1 ... xn.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   499
 * to one of
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   500
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   501
 *      !!v1 ... vn. v1=M1 ==> ... ==> vn=Mn
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   502
 *      ==> ((%v1...vn. Q) v1 ... vn = g v1 ... vn.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   503
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   504
 * This prevents name problems in extraction, and helps the result to read
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   505
 * better. There is a problem with varstructs, since they can introduce more
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   506
 * than n variables, and some extra reasoning needs to be done.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   507
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   508
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   509
fun get ([],_,L) = rev L
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   510
  | get (ant::rst,n,L) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   511
      case (list_break_all ant)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   512
        of ([],_) => get (rst, n+1,L)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   513
         | (vlist,body) =>
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   514
            let val eq = Logic.strip_imp_concl body
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   515
                val (f,args) = S.strip_comb (get_lhs eq)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   516
                val (vstrl,_) = S.strip_abs f
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   517
                val names  = variantlist (map (#1 o dest_Free) vstrl,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   518
                                          add_term_names(body, []))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   519
            in get (rst, n+1, (names,n)::L) end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   520
            handle TERM _ => get (rst, n+1, L)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   521
              | U.ERR _ => get (rst, n+1, L);
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   522
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   523
(* Note: rename_params_rule counts from 1, not 0 *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   524
fun rename thm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   525
  let val {prop,sign,...} = rep_thm thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   526
      val tych = cterm_of sign
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   527
      val ants = Logic.strip_imp_prems prop
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   528
      val news = get (ants,1,[])
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   529
  in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   530
  U.rev_itlist rename_params_rule news thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   531
  end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   532
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   533
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   534
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   535
 * Beta-conversion to the rhs of an equation (taken from hol90/drule.sml)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   536
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   537
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   538
fun list_beta_conv tm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   539
  let fun rbeta th = Thm.transitive th (beta_conversion false (#2(D.dest_eq(cconcl th))))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   540
      fun iter [] = Thm.reflexive tm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   541
        | iter (v::rst) = rbeta (combination(iter rst) (Thm.reflexive v))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   542
  in iter  end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   543
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   544
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   545
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   546
 * Trace information for the rewriter
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   547
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   548
val term_ref = ref[] : term list ref
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   549
val mss_ref = ref [] : meta_simpset list ref;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   550
val thm_ref = ref [] : thm list ref;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   551
val tracing = ref false;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   552
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   553
fun say s = if !tracing then writeln s else ();
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   554
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   555
fun print_thms s L =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   556
  say (cat_lines (s :: map string_of_thm L));
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   557
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   558
fun print_cterms s L =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   559
  say (cat_lines (s :: map string_of_cterm L));
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   560
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   561
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   562
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   563
 * General abstraction handlers, should probably go in USyntax.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   564
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   565
fun mk_aabs (vstr, body) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   566
  S.mk_abs {Bvar = vstr, Body = body}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   567
  handle U.ERR _ => S.mk_pabs {varstruct = vstr, body = body};
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   568
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   569
fun list_mk_aabs (vstrl,tm) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   570
    U.itlist (fn vstr => fn tm => mk_aabs(vstr,tm)) vstrl tm;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   571
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   572
fun dest_aabs used tm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   573
   let val ({Bvar,Body}, used') = S.dest_abs used tm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   574
   in (Bvar, Body, used') end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   575
   handle U.ERR _ =>
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   576
     let val {varstruct, body, used} = S.dest_pabs used tm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   577
     in (varstruct, body, used) end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   578
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   579
fun strip_aabs used tm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   580
   let val (vstr, body, used') = dest_aabs used tm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   581
       val (bvs, core, used'') = strip_aabs used' body
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   582
   in (vstr::bvs, core, used'') end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   583
   handle U.ERR _ => ([], tm, used);
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   584
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   585
fun dest_combn tm 0 = (tm,[])
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   586
  | dest_combn tm n =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   587
     let val {Rator,Rand} = S.dest_comb tm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   588
         val (f,rands) = dest_combn Rator (n-1)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   589
     in (f,Rand::rands)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   590
     end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   591
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   592
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   593
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   594
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   595
local fun dest_pair M = let val {fst,snd} = S.dest_pair M in (fst,snd) end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   596
      fun mk_fst tm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   597
          let val ty as Type("*", [fty,sty]) = type_of tm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   598
          in  Const ("fst", ty --> fty) $ tm  end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   599
      fun mk_snd tm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   600
          let val ty as Type("*", [fty,sty]) = type_of tm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   601
          in  Const ("snd", ty --> sty) $ tm  end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   602
in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   603
fun XFILL tych x vstruct =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   604
  let fun traverse p xocc L =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   605
        if (is_Free p)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   606
        then tych xocc::L
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   607
        else let val (p1,p2) = dest_pair p
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   608
             in traverse p1 (mk_fst xocc) (traverse p2  (mk_snd xocc) L)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   609
             end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   610
  in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   611
  traverse vstruct x []
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   612
end end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   613
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   614
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   615
 * Replace a free tuple (vstr) by a universally quantified variable (a).
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   616
 * Note that the notion of "freeness" for a tuple is different than for a
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   617
 * variable: if variables in the tuple also occur in any other place than
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   618
 * an occurrences of the tuple, they aren't "free" (which is thus probably
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   619
 *  the wrong word to use).
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   620
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   621
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   622
fun VSTRUCT_ELIM tych a vstr th =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   623
  let val L = S.free_vars_lr vstr
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   624
      val bind1 = tych (HOLogic.mk_Trueprop (HOLogic.mk_eq(a,vstr)))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   625
      val thm1 = implies_intr bind1 (SUBS [SYM(assume bind1)] th)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   626
      val thm2 = forall_intr_list (map tych L) thm1
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   627
      val thm3 = forall_elim_list (XFILL tych a vstr) thm2
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   628
  in refl RS
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   629
     rewrite_rule [Thm.symmetric (surjective_pairing RS eq_reflection)] thm3
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   630
  end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   631
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   632
fun PGEN tych a vstr th =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   633
  let val a1 = tych a
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   634
      val vstr1 = tych vstr
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   635
  in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   636
  forall_intr a1
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   637
     (if (is_Free vstr)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   638
      then cterm_instantiate [(vstr1,a1)] th
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   639
      else VSTRUCT_ELIM tych a vstr th)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   640
  end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   641
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   642
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   643
(*---------------------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   644
 * Takes apart a paired beta-redex, looking like "(\(x,y).N) vstr", into
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   645
 *
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   646
 *     (([x,y],N),vstr)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   647
 *---------------------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   648
fun dest_pbeta_redex used M n =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   649
  let val (f,args) = dest_combn M n
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   650
      val dummy = dest_aabs used f
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   651
  in (strip_aabs used f,args)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   652
  end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   653
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   654
fun pbeta_redex M n = can (U.C (dest_pbeta_redex []) n) M;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   655
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   656
fun dest_impl tm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   657
  let val ants = Logic.strip_imp_prems tm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   658
      val eq = Logic.strip_imp_concl tm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   659
  in (ants,get_lhs eq)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   660
  end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   661
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   662
fun restricted t = is_some (S.find_term
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   663
                            (fn (Const("Wellfounded_Recursion.cut",_)) =>true | _ => false)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   664
                            t)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   665
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   666
fun CONTEXT_REWRITE_RULE (func, G, cut_lemma, congs) th =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   667
 let val globals = func::G
10918
9679326489cd renamed Product_Type.split to split_conv;
wenzelm
parents: 10781
diff changeset
   668
     val pbeta_reduce = simpl_conv empty_ss [split_conv RS eq_reflection];
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   669
     val tc_list = ref[]: term list ref
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   670
     val dummy = term_ref := []
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   671
     val dummy = thm_ref  := []
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   672
     val dummy = mss_ref  := []
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   673
     val cut_lemma' = cut_lemma RS eq_reflection
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   674
     fun prover used mss thm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   675
     let fun cong_prover mss thm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   676
         let val dummy = say "cong_prover:"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   677
             val cntxt = prems_of_mss mss
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   678
             val dummy = print_thms "cntxt:" cntxt
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   679
             val dummy = say "cong rule:"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   680
             val dummy = say (string_of_thm thm)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   681
             val dummy = thm_ref := (thm :: !thm_ref)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   682
             val dummy = mss_ref := (mss :: !mss_ref)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   683
             (* Unquantified eliminate *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   684
             fun uq_eliminate (thm,imp,sign) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   685
                 let val tych = cterm_of sign
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   686
                     val dummy = print_cterms "To eliminate:" [tych imp]
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   687
                     val ants = map tych (Logic.strip_imp_prems imp)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   688
                     val eq = Logic.strip_imp_concl imp
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   689
                     val lhs = tych(get_lhs eq)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   690
                     val mss' = add_prems(mss, map ASSUME ants)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   691
                     val lhs_eq_lhs1 = MetaSimplifier.rewrite_cterm (false,true,false) (prover used) mss' lhs
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   692
                       handle U.ERR _ => Thm.reflexive lhs
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   693
                     val dummy = print_thms "proven:" [lhs_eq_lhs1]
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   694
                     val lhs_eq_lhs2 = implies_intr_list ants lhs_eq_lhs1
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   695
                     val lhs_eeq_lhs2 = lhs_eq_lhs2 RS meta_eq_to_obj_eq
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   696
                  in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   697
                  lhs_eeq_lhs2 COMP thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   698
                  end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   699
             fun pq_eliminate (thm,sign,vlist,imp_body,lhs_eq) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   700
              let val ((vstrl, _, used'), args) = dest_pbeta_redex used lhs_eq (length vlist)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   701
                  val dummy = assert (forall (op aconv)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   702
                                      (ListPair.zip (vlist, args)))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   703
                               "assertion failed in CONTEXT_REWRITE_RULE"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   704
                  val imp_body1 = subst_free (ListPair.zip (args, vstrl))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   705
                                             imp_body
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   706
                  val tych = cterm_of sign
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   707
                  val ants1 = map tych (Logic.strip_imp_prems imp_body1)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   708
                  val eq1 = Logic.strip_imp_concl imp_body1
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   709
                  val Q = get_lhs eq1
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   710
                  val QeqQ1 = pbeta_reduce (tych Q)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   711
                  val Q1 = #2(D.dest_eq(cconcl QeqQ1))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   712
                  val mss' = add_prems(mss, map ASSUME ants1)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   713
                  val Q1eeqQ2 = MetaSimplifier.rewrite_cterm (false,true,false) (prover used') mss' Q1
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   714
                                handle U.ERR _ => Thm.reflexive Q1
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   715
                  val Q2 = #2 (Logic.dest_equals (#prop(rep_thm Q1eeqQ2)))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   716
                  val Q3 = tych(list_comb(list_mk_aabs(vstrl,Q2),vstrl))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   717
                  val Q2eeqQ3 = Thm.symmetric(pbeta_reduce Q3 RS eq_reflection)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   718
                  val thA = Thm.transitive(QeqQ1 RS eq_reflection) Q1eeqQ2
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   719
                  val QeeqQ3 = Thm.transitive thA Q2eeqQ3 handle THM _ =>
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   720
                               ((Q2eeqQ3 RS meta_eq_to_obj_eq)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   721
                                RS ((thA RS meta_eq_to_obj_eq) RS trans))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   722
                                RS eq_reflection
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   723
                  val impth = implies_intr_list ants1 QeeqQ3
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   724
                  val impth1 = impth RS meta_eq_to_obj_eq
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   725
                  (* Need to abstract *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   726
                  val ant_th = U.itlist2 (PGEN tych) args vstrl impth1
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   727
              in ant_th COMP thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   728
              end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   729
             fun q_eliminate (thm,imp,sign) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   730
              let val (vlist, imp_body, used') = strip_all used imp
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   731
                  val (ants,Q) = dest_impl imp_body
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   732
              in if (pbeta_redex Q) (length vlist)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   733
                 then pq_eliminate (thm,sign,vlist,imp_body,Q)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   734
                 else
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   735
                 let val tych = cterm_of sign
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   736
                     val ants1 = map tych ants
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   737
                     val mss' = add_prems(mss, map ASSUME ants1)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   738
                     val Q_eeq_Q1 = MetaSimplifier.rewrite_cterm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   739
                        (false,true,false) (prover used') mss' (tych Q)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   740
                      handle U.ERR _ => Thm.reflexive (tych Q)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   741
                     val lhs_eeq_lhs2 = implies_intr_list ants1 Q_eeq_Q1
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   742
                     val lhs_eq_lhs2 = lhs_eeq_lhs2 RS meta_eq_to_obj_eq
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   743
                     val ant_th = forall_intr_list(map tych vlist)lhs_eq_lhs2
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   744
                 in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   745
                 ant_th COMP thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   746
              end end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   747
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   748
             fun eliminate thm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   749
               case (rep_thm thm)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   750
               of {prop = (Const("==>",_) $ imp $ _), sign, ...} =>
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   751
                   eliminate
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   752
                    (if not(is_all imp)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   753
                     then uq_eliminate (thm,imp,sign)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   754
                     else q_eliminate (thm,imp,sign))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   755
                            (* Assume that the leading constant is ==,   *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   756
                | _ => thm  (* if it is not a ==>                        *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   757
         in Some(eliminate (rename thm)) end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   758
         handle U.ERR _ => None    (* FIXME handle THM as well?? *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   759
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   760
        fun restrict_prover mss thm =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   761
          let val dummy = say "restrict_prover:"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   762
              val cntxt = rev(prems_of_mss mss)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   763
              val dummy = print_thms "cntxt:" cntxt
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   764
              val {prop = Const("==>",_) $ (Const("Trueprop",_) $ A) $ _,
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   765
                   sign,...} = rep_thm thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   766
              fun genl tm = let val vlist = gen_rems (op aconv)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   767
                                           (add_term_frees(tm,[]), globals)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   768
                            in U.itlist Forall vlist tm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   769
                            end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   770
              (*--------------------------------------------------------------
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   771
               * This actually isn't quite right, since it will think that
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   772
               * not-fully applied occs. of "f" in the context mean that the
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   773
               * current call is nested. The real solution is to pass in a
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   774
               * term "f v1..vn" which is a pattern that any full application
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   775
               * of "f" will match.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   776
               *-------------------------------------------------------------*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   777
              val func_name = #1(dest_Const func)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   778
              fun is_func (Const (name,_)) = (name = func_name)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   779
                | is_func _                = false
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   780
              val rcontext = rev cntxt
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   781
              val cncl = HOLogic.dest_Trueprop o #prop o rep_thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   782
              val antl = case rcontext of [] => []
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   783
                         | _   => [S.list_mk_conj(map cncl rcontext)]
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   784
              val TC = genl(S.list_mk_imp(antl, A))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   785
              val dummy = print_cterms "func:" [cterm_of sign func]
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   786
              val dummy = print_cterms "TC:"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   787
                              [cterm_of sign (HOLogic.mk_Trueprop TC)]
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   788
              val dummy = tc_list := (TC :: !tc_list)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   789
              val nestedp = is_some (S.find_term is_func TC)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   790
              val dummy = if nestedp then say "nested" else say "not_nested"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   791
              val dummy = term_ref := ([func,TC]@(!term_ref))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   792
              val th' = if nestedp then raise RULES_ERR "solver" "nested function"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   793
                        else let val cTC = cterm_of sign
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   794
                                              (HOLogic.mk_Trueprop TC)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   795
                             in case rcontext of
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   796
                                [] => SPEC_ALL(ASSUME cTC)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   797
                               | _ => MP (SPEC_ALL (ASSUME cTC))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   798
                                         (LIST_CONJ rcontext)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   799
                             end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   800
              val th'' = th' RS thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   801
          in Some (th'')
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   802
          end handle U.ERR _ => None    (* FIXME handle THM as well?? *)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   803
    in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   804
    (if (is_cong thm) then cong_prover else restrict_prover) mss thm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   805
    end
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   806
    val ctm = cprop_of th
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   807
    val names = add_term_names (term_of ctm, [])
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   808
    val th1 = MetaSimplifier.rewrite_cterm(false,true,false)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   809
      (prover names) (add_congs(mss_of [cut_lemma'], congs)) ctm
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   810
    val th2 = equal_elim th1 th
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   811
 in
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   812
 (th2, filter (not o restricted) (!tc_list))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   813
 end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   814
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   815
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   816
fun prove (ptm, tac) =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   817
  let val result =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   818
    Library.transform_error (fn () =>
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   819
      Goals.prove_goalw_cterm [] ptm (fn _ => [tac])) ()
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   820
    handle ERROR_MESSAGE msg => (warning msg; raise RULES_ERR "prove" msg)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   821
  in #1 (freeze_thaw result) end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   822
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   823
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
   824
end;