src/HOL/Tools/TFL/rules.ML
changeset 60363 5568b16aa477
parent 60335 edac62cd7bde
child 60365 3e28769ba2b6
equal deleted inserted replaced
60362:befdc10ebb42 60363:5568b16aa477
    19   val DISCH: cterm -> thm -> thm
    19   val DISCH: cterm -> thm -> thm
    20   val UNDISCH: thm  -> thm
    20   val UNDISCH: thm  -> thm
    21   val SPEC: cterm -> thm -> thm
    21   val SPEC: cterm -> thm -> thm
    22   val ISPEC: cterm -> thm -> thm
    22   val ISPEC: cterm -> thm -> thm
    23   val ISPECL: cterm list -> thm -> thm
    23   val ISPECL: cterm list -> thm -> thm
    24   val GEN: cterm -> thm -> thm
    24   val GEN: Proof.context -> cterm -> thm -> thm
    25   val GENL: cterm list -> thm -> thm
    25   val GENL: Proof.context -> cterm list -> thm -> thm
    26   val LIST_CONJ: thm list -> thm
    26   val LIST_CONJ: thm list -> thm
    27 
    27 
    28   val SYM: thm -> thm
    28   val SYM: thm -> thm
    29   val DISCH_ALL: thm -> thm
    29   val DISCH_ALL: thm -> thm
    30   val FILTER_DISCH_ALL: (term -> bool) -> thm -> thm
    30   val FILTER_DISCH_ALL: (term -> bool) -> thm -> thm
    31   val SPEC_ALL: thm -> thm
    31   val SPEC_ALL: thm -> thm
    32   val GEN_ALL: thm -> thm
    32   val GEN_ALL: Proof.context -> thm -> thm
    33   val IMP_TRANS: thm -> thm -> thm
    33   val IMP_TRANS: thm -> thm -> thm
    34   val PROVE_HYP: thm -> thm -> thm
    34   val PROVE_HYP: thm -> thm -> thm
    35 
    35 
    36   val CHOOSE: Proof.context -> cterm * thm -> thm -> thm
    36   val CHOOSE: Proof.context -> cterm * thm -> thm -> thm
    37   val EXISTS: cterm * cterm -> thm -> thm
    37   val EXISTS: cterm * cterm -> thm -> thm
    38   val EXISTL: cterm list -> thm -> thm
    38   val EXISTL: cterm list -> thm -> thm
    39   val IT_EXISTS: (cterm*cterm) list -> thm -> thm
    39   val IT_EXISTS: Proof.context -> (cterm * cterm) list -> thm -> thm
    40 
    40 
    41   val EVEN_ORS: thm list -> thm list
    41   val EVEN_ORS: thm list -> thm list
    42   val DISJ_CASESL: thm -> thm list -> thm
    42   val DISJ_CASESL: thm -> thm list -> thm
    43 
    43 
    44   val list_beta_conv: cterm -> cterm list -> thm
    44   val list_beta_conv: cterm -> cterm list -> thm
   156 
   156 
   157 
   157 
   158 (*----------------------------------------------------------------------------
   158 (*----------------------------------------------------------------------------
   159  *        Disjunction
   159  *        Disjunction
   160  *---------------------------------------------------------------------------*)
   160  *---------------------------------------------------------------------------*)
   161 local val thy = Thm.theory_of_thm disjI1
   161 local
   162       val prop = Thm.prop_of disjI1
   162   val prop = Thm.prop_of disjI1
   163       val [P,Q] = Misc_Legacy.term_vars prop
   163   val [P,Q] = Misc_Legacy.term_vars prop
   164       val disj1 = Thm.forall_intr (Thm.global_cterm_of thy Q) disjI1
   164   val disj1 = Thm.forall_intr (Thm.cterm_of @{context} Q) disjI1
   165 in
   165 in
   166 fun DISJ1 thm tm = thm RS (Thm.forall_elim (Dcterm.drop_prop tm) disj1)
   166 fun DISJ1 thm tm = thm RS (Thm.forall_elim (Dcterm.drop_prop tm) disj1)
   167   handle THM (msg, _, _) => raise RULES_ERR "DISJ1" msg;
   167   handle THM (msg, _, _) => raise RULES_ERR "DISJ1" msg;
   168 end;
   168 end;
   169 
   169 
   170 local val thy = Thm.theory_of_thm disjI2
   170 local
   171       val prop = Thm.prop_of disjI2
   171   val prop = Thm.prop_of disjI2
   172       val [P,Q] = Misc_Legacy.term_vars prop
   172   val [P,Q] = Misc_Legacy.term_vars prop
   173       val disj2 = Thm.forall_intr (Thm.global_cterm_of thy P) disjI2
   173   val disj2 = Thm.forall_intr (Thm.cterm_of @{context} P) disjI2
   174 in
   174 in
   175 fun DISJ2 tm thm = thm RS (Thm.forall_elim (Dcterm.drop_prop tm) disj2)
   175 fun DISJ2 tm thm = thm RS (Thm.forall_elim (Dcterm.drop_prop tm) disj2)
   176   handle THM (msg, _, _) => raise RULES_ERR "DISJ2" msg;
   176   handle THM (msg, _, _) => raise RULES_ERR "DISJ2" msg;
   177 end;
   177 end;
   178 
   178 
   260 
   260 
   261 (*----------------------------------------------------------------------------
   261 (*----------------------------------------------------------------------------
   262  *        Universals
   262  *        Universals
   263  *---------------------------------------------------------------------------*)
   263  *---------------------------------------------------------------------------*)
   264 local (* this is fragile *)
   264 local (* this is fragile *)
   265       val thy = Thm.theory_of_thm spec
   265   val prop = Thm.prop_of spec
   266       val prop = Thm.prop_of spec
   266   val x = hd (tl (Misc_Legacy.term_vars prop))
   267       val x = hd (tl (Misc_Legacy.term_vars prop))
   267   val cTV = Thm.ctyp_of @{context} (type_of x)
   268       val cTV = Thm.global_ctyp_of thy (type_of x)
   268   val gspec = Thm.forall_intr (Thm.cterm_of @{context} x) spec
   269       val gspec = Thm.forall_intr (Thm.global_cterm_of thy x) spec
       
   270 in
   269 in
   271 fun SPEC tm thm =
   270 fun SPEC tm thm =
   272    let val gspec' = Drule.instantiate_normalize ([(cTV, Thm.ctyp_of_cterm tm)], []) gspec
   271    let val gspec' = Drule.instantiate_normalize ([(cTV, Thm.ctyp_of_cterm tm)], []) gspec
   273    in thm RS (Thm.forall_elim tm gspec') end
   272    in thm RS (Thm.forall_elim tm gspec') end
   274 end;
   273 end;
   277 
   276 
   278 val ISPEC = SPEC
   277 val ISPEC = SPEC
   279 val ISPECL = fold ISPEC;
   278 val ISPECL = fold ISPEC;
   280 
   279 
   281 (* Not optimized! Too complicated. *)
   280 (* Not optimized! Too complicated. *)
   282 local val thy = Thm.theory_of_thm allI
   281 local
   283       val prop = Thm.prop_of allI
   282   val prop = Thm.prop_of allI
   284       val [P] = Misc_Legacy.add_term_vars (prop, [])
   283   val [P] = Misc_Legacy.add_term_vars (prop, [])
   285       fun cty_theta s = map (fn (i, (S, ty)) => (Thm.global_ctyp_of s (TVar (i, S)), Thm.global_ctyp_of s ty))
   284   fun cty_theta ctxt = map (fn (i, (S, ty)) => apply2 (Thm.ctyp_of ctxt) (TVar (i, S), ty))
   286       fun ctm_theta s = map (fn (i, (_, tm2)) =>
   285   fun ctm_theta ctxt =
   287                              let val ctm2 = Thm.global_cterm_of s tm2
   286     map (fn (i, (_, tm2)) =>
   288                              in (Thm.global_cterm_of s (Var(i, Thm.typ_of_cterm ctm2)), ctm2)
   287       let val ctm2 = Thm.cterm_of ctxt tm2
   289                              end)
   288       in (Thm.cterm_of ctxt (Var (i, Thm.typ_of_cterm ctm2)), ctm2) end)
   290       fun certify s (ty_theta,tm_theta) =
   289   fun certify ctxt (ty_theta,tm_theta) =
   291         (cty_theta s (Vartab.dest ty_theta),
   290     (cty_theta ctxt (Vartab.dest ty_theta),
   292          ctm_theta s (Vartab.dest tm_theta))
   291      ctm_theta ctxt (Vartab.dest tm_theta))
   293 in
   292 in
   294 fun GEN v th =
   293 fun GEN ctxt v th =
   295    let val gth = Thm.forall_intr v th
   294    let val gth = Thm.forall_intr v th
   296        val thy = Thm.theory_of_thm gth
   295        val thy = Proof_Context.theory_of ctxt
   297        val Const(@{const_name Pure.all},_)$Abs(x,ty,rst) = Thm.prop_of gth
   296        val Const(@{const_name Pure.all},_)$Abs(x,ty,rst) = Thm.prop_of gth
   298        val P' = Abs(x,ty, HOLogic.dest_Trueprop rst)  (* get rid of trueprop *)
   297        val P' = Abs(x,ty, HOLogic.dest_Trueprop rst)  (* get rid of trueprop *)
   299        val theta = Pattern.match thy (P,P') (Vartab.empty, Vartab.empty);
   298        val theta = Pattern.match thy (P,P') (Vartab.empty, Vartab.empty);
   300        val allI2 = Drule.instantiate_normalize (certify thy theta) allI
   299        val allI2 = Drule.instantiate_normalize (certify ctxt theta) allI
   301        val thm = Thm.implies_elim allI2 gth
   300        val thm = Thm.implies_elim allI2 gth
   302        val tp $ (A $ Abs(_,_,M)) = Thm.prop_of thm
   301        val tp $ (A $ Abs(_,_,M)) = Thm.prop_of thm
   303        val prop' = tp $ (A $ Abs(x,ty,M))
   302        val prop' = tp $ (A $ Abs(x,ty,M))
   304    in ALPHA thm (Thm.global_cterm_of thy prop')
   303    in ALPHA thm (Thm.cterm_of ctxt prop') end
   305    end
       
   306 end;
   304 end;
   307 
   305 
   308 val GENL = fold_rev GEN;
   306 fun GENL ctxt = fold_rev (GEN ctxt);
   309 
   307 
   310 fun GEN_ALL thm =
   308 fun GEN_ALL ctxt thm =
   311    let val thy = Thm.theory_of_thm thm
   309   let
   312        val prop = Thm.prop_of thm
   310     val prop = Thm.prop_of thm
   313        val tycheck = Thm.global_cterm_of thy
   311     val vlist = map (Thm.cterm_of ctxt) (Misc_Legacy.add_term_vars (prop, []))
   314        val vlist = map tycheck (Misc_Legacy.add_term_vars (prop, []))
   312   in GENL ctxt vlist thm end;
   315   in GENL vlist thm
       
   316   end;
       
   317 
   313 
   318 
   314 
   319 fun MATCH_MP th1 th2 =
   315 fun MATCH_MP th1 th2 =
   320    if (Dcterm.is_forall (Dcterm.drop_prop(cconcl th1)))
   316    if (Dcterm.is_forall (Dcterm.drop_prop(cconcl th1)))
   321    then MATCH_MP (th1 RS spec) th2
   317    then MATCH_MP (th1 RS spec) th2
   342     val lam = #2 (Dcterm.dest_comb (Dcterm.drop_prop (cconcl exth)))
   338     val lam = #2 (Dcterm.dest_comb (Dcterm.drop_prop (cconcl exth)))
   343     val redex = Dcterm.capply lam fvar
   339     val redex = Dcterm.capply lam fvar
   344     val t$u = Thm.term_of redex
   340     val t$u = Thm.term_of redex
   345     val residue = Thm.cterm_of ctxt (Term.betapply (t, u))
   341     val residue = Thm.cterm_of ctxt (Term.betapply (t, u))
   346   in
   342   in
   347     GEN fvar (DISCH residue fact) RS (exth RS Thms.choose_thm)
   343     GEN ctxt fvar (DISCH residue fact) RS (exth RS Thms.choose_thm)
   348       handle THM (msg, _, _) => raise RULES_ERR "CHOOSE" msg
   344       handle THM (msg, _, _) => raise RULES_ERR "CHOOSE" msg
   349   end;
   345   end;
   350 
   346 
   351 local val thy = Thm.theory_of_thm exI
   347 local
   352       val prop = Thm.prop_of exI
   348   val prop = Thm.prop_of exI
   353       val [P,x] = Misc_Legacy.term_vars prop
   349   val [P, x] = map (Thm.cterm_of @{context}) (Misc_Legacy.term_vars prop)
   354 in
   350 in
   355 fun EXISTS (template,witness) thm =
   351 fun EXISTS (template,witness) thm =
   356    let val thy = Thm.theory_of_thm thm
   352   let val abstr = #2 (Dcterm.dest_comb template) in
   357        val prop = Thm.prop_of thm
   353     thm RS (cterm_instantiate [(P, abstr), (x, witness)] exI)
   358        val P' = Thm.global_cterm_of thy P
   354       handle THM (msg, _, _) => raise RULES_ERR "EXISTS" msg
   359        val x' = Thm.global_cterm_of thy x
   355   end
   360        val abstr = #2 (Dcterm.dest_comb template)
       
   361    in
       
   362    thm RS (cterm_instantiate[(P',abstr), (x',witness)] exI)
       
   363      handle THM (msg, _, _) => raise RULES_ERR "EXISTS" msg
       
   364    end
       
   365 end;
   356 end;
   366 
   357 
   367 (*----------------------------------------------------------------------------
   358 (*----------------------------------------------------------------------------
   368  *
   359  *
   369  *         A |- M
   360  *         A |- M
   384  *       A |- ?y_1...y_n. M
   375  *       A |- ?y_1...y_n. M
   385  *
   376  *
   386  *---------------------------------------------------------------------------*)
   377  *---------------------------------------------------------------------------*)
   387 (* Could be improved, but needs "subst_free" for certified terms *)
   378 (* Could be improved, but needs "subst_free" for certified terms *)
   388 
   379 
   389 fun IT_EXISTS blist th =
   380 fun IT_EXISTS ctxt blist th =
   390    let val thy = Thm.theory_of_thm th
   381   let
   391        val tych = Thm.global_cterm_of thy
   382     val blist' = map (apply2 Thm.term_of) blist
   392        val blist' = map (apply2 Thm.term_of) blist
   383     fun ex v M = Thm.cterm_of ctxt (USyntax.mk_exists{Bvar=v,Body = M})
   393        fun ex v M  = Thm.global_cterm_of thy (USyntax.mk_exists{Bvar=v,Body = M})
       
   394 
       
   395   in
   384   in
   396   fold_rev (fn (b as (r1,r2)) => fn thm =>
   385     fold_rev (fn (b as (r1,r2)) => fn thm =>
   397         EXISTS(ex r2 (subst_free [b]
   386         EXISTS(ex r2 (subst_free [b]
   398                    (HOLogic.dest_Trueprop(Thm.prop_of thm))), tych r1)
   387                    (HOLogic.dest_Trueprop(Thm.prop_of thm))), Thm.cterm_of ctxt r1)
   399               thm)
   388               thm)
   400        blist' th
   389        blist' th
   401   end;
   390   end;
   402 
   391 
   403 (*---------------------------------------------------------------------------
   392 (*---------------------------------------------------------------------------
   507             handle TERM _ => get (rst, n+1, L)
   496             handle TERM _ => get (rst, n+1, L)
   508               | Utils.ERR _ => get (rst, n+1, L);
   497               | Utils.ERR _ => get (rst, n+1, L);
   509 
   498 
   510 (* Note: Thm.rename_params_rule counts from 1, not 0 *)
   499 (* Note: Thm.rename_params_rule counts from 1, not 0 *)
   511 fun rename thm =
   500 fun rename thm =
   512   let val thy = Thm.theory_of_thm thm
   501   let
   513       val tych = Thm.global_cterm_of thy
   502     val ants = Logic.strip_imp_prems (Thm.prop_of thm)
   514       val ants = Logic.strip_imp_prems (Thm.prop_of thm)
   503     val news = get (ants,1,[])
   515       val news = get (ants,1,[])
   504   in fold Thm.rename_params_rule news thm end;
   516   in
       
   517   fold Thm.rename_params_rule news thm
       
   518   end;
       
   519 
   505 
   520 
   506 
   521 (*---------------------------------------------------------------------------
   507 (*---------------------------------------------------------------------------
   522  * Beta-conversion to the rhs of an equation (taken from hol90/drule.sml)
   508  * Beta-conversion to the rhs of an equation (taken from hol90/drule.sml)
   523  *---------------------------------------------------------------------------*)
   509  *---------------------------------------------------------------------------*)
   735                 | _ => thm  (* if it is not a ==>                        *)
   721                 | _ => thm  (* if it is not a ==>                        *)
   736          in SOME(eliminate (rename thm)) end
   722          in SOME(eliminate (rename thm)) end
   737          handle Utils.ERR _ => NONE    (* FIXME handle THM as well?? *)
   723          handle Utils.ERR _ => NONE    (* FIXME handle THM as well?? *)
   738 
   724 
   739         fun restrict_prover ctxt thm =
   725         fun restrict_prover ctxt thm =
   740           let val dummy = say "restrict_prover:"
   726           let val _ = say "restrict_prover:"
   741               val cntxt = rev (Simplifier.prems_of ctxt)
   727               val cntxt = rev (Simplifier.prems_of ctxt)
   742               val dummy = print_thms ctxt "cntxt:" cntxt
   728               val _ = print_thms ctxt "cntxt:" cntxt
   743               val thy = Thm.theory_of_thm thm
       
   744               val Const(@{const_name Pure.imp},_) $ (Const(@{const_name Trueprop},_) $ A) $ _ =
   729               val Const(@{const_name Pure.imp},_) $ (Const(@{const_name Trueprop},_) $ A) $ _ =
   745                 Thm.prop_of thm
   730                 Thm.prop_of thm
   746               fun genl tm = let val vlist = subtract (op aconv) globals
   731               fun genl tm = let val vlist = subtract (op aconv) globals
   747                                            (Misc_Legacy.add_term_frees(tm,[]))
   732                                            (Misc_Legacy.add_term_frees(tm,[]))
   748                             in fold_rev Forall vlist tm
   733                             in fold_rev Forall vlist tm
   760               val rcontext = rev cntxt
   745               val rcontext = rev cntxt
   761               val cncl = HOLogic.dest_Trueprop o Thm.prop_of
   746               val cncl = HOLogic.dest_Trueprop o Thm.prop_of
   762               val antl = case rcontext of [] => []
   747               val antl = case rcontext of [] => []
   763                          | _   => [USyntax.list_mk_conj(map cncl rcontext)]
   748                          | _   => [USyntax.list_mk_conj(map cncl rcontext)]
   764               val TC = genl(USyntax.list_mk_imp(antl, A))
   749               val TC = genl(USyntax.list_mk_imp(antl, A))
   765               val dummy = print_cterm ctxt "func:" (Thm.global_cterm_of thy func)
   750               val dummy = print_cterm ctxt "func:" (Thm.cterm_of ctxt func)
   766               val dummy = print_cterm ctxt "TC:" (Thm.global_cterm_of thy (HOLogic.mk_Trueprop TC))
   751               val dummy = print_cterm ctxt "TC:" (Thm.cterm_of ctxt (HOLogic.mk_Trueprop TC))
   767               val dummy = tc_list := (TC :: !tc_list)
   752               val dummy = tc_list := (TC :: !tc_list)
   768               val nestedp = is_some (USyntax.find_term is_func TC)
   753               val nestedp = is_some (USyntax.find_term is_func TC)
   769               val dummy = if nestedp then say "nested" else say "not_nested"
   754               val dummy = if nestedp then say "nested" else say "not_nested"
   770               val th' = if nestedp then raise RULES_ERR "solver" "nested function"
   755               val th' = if nestedp then raise RULES_ERR "solver" "nested function"
   771                         else let val cTC = Thm.global_cterm_of thy
   756                         else let val cTC = Thm.cterm_of ctxt (HOLogic.mk_Trueprop TC)
   772                                               (HOLogic.mk_Trueprop TC)
       
   773                              in case rcontext of
   757                              in case rcontext of
   774                                 [] => SPEC_ALL(ASSUME cTC)
   758                                 [] => SPEC_ALL(ASSUME cTC)
   775                                | _ => MP (SPEC_ALL (ASSUME cTC))
   759                                | _ => MP (SPEC_ALL (ASSUME cTC))
   776                                          (LIST_CONJ rcontext)
   760                                          (LIST_CONJ rcontext)
   777                              end
   761                              end