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