src/HOL/Tools/TFL/rules.ML
changeset 59621 291934bac95e
parent 59586 ddf6deaadfe8
child 60329 e85ed7a36b2f
equal deleted inserted replaced
59620:92d7d8e4f1bf 59621:291934bac95e
   159  *        Disjunction
   159  *        Disjunction
   160  *---------------------------------------------------------------------------*)
   160  *---------------------------------------------------------------------------*)
   161 local val thy = Thm.theory_of_thm disjI1
   161 local val thy = Thm.theory_of_thm disjI1
   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.cterm_of thy Q) disjI1
   164       val disj1 = Thm.forall_intr (Thm.global_cterm_of thy 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 val thy = Thm.theory_of_thm disjI2
   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.cterm_of thy P) disjI2
   173       val disj2 = Thm.forall_intr (Thm.global_cterm_of thy 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 
   263  *---------------------------------------------------------------------------*)
   263  *---------------------------------------------------------------------------*)
   264 local (* this is fragile *)
   264 local (* this is fragile *)
   265       val thy = Thm.theory_of_thm spec
   265       val thy = Thm.theory_of_thm spec
   266       val prop = Thm.prop_of spec
   266       val prop = Thm.prop_of spec
   267       val x = hd (tl (Misc_Legacy.term_vars prop))
   267       val x = hd (tl (Misc_Legacy.term_vars prop))
   268       val cTV = Thm.ctyp_of thy (type_of x)
   268       val cTV = Thm.global_ctyp_of thy (type_of x)
   269       val gspec = Thm.forall_intr (Thm.cterm_of thy x) spec
   269       val gspec = Thm.forall_intr (Thm.global_cterm_of thy x) spec
   270 in
   270 in
   271 fun SPEC tm thm =
   271 fun SPEC tm thm =
   272    let val gspec' = Drule.instantiate_normalize ([(cTV, Thm.ctyp_of_cterm tm)], []) gspec
   272    let val gspec' = Drule.instantiate_normalize ([(cTV, Thm.ctyp_of_cterm tm)], []) gspec
   273    in thm RS (Thm.forall_elim tm gspec') end
   273    in thm RS (Thm.forall_elim tm gspec') end
   274 end;
   274 end;
   280 
   280 
   281 (* Not optimized! Too complicated. *)
   281 (* Not optimized! Too complicated. *)
   282 local val thy = Thm.theory_of_thm allI
   282 local val thy = Thm.theory_of_thm allI
   283       val prop = Thm.prop_of allI
   283       val prop = Thm.prop_of allI
   284       val [P] = Misc_Legacy.add_term_vars (prop, [])
   284       val [P] = Misc_Legacy.add_term_vars (prop, [])
   285       fun cty_theta s = map (fn (i, (S, ty)) => (Thm.ctyp_of s (TVar (i, S)), Thm.ctyp_of s ty))
   285       fun cty_theta s = map (fn (i, (S, ty)) => (Thm.global_ctyp_of s (TVar (i, S)), Thm.global_ctyp_of s ty))
   286       fun ctm_theta s = map (fn (i, (_, tm2)) =>
   286       fun ctm_theta s = map (fn (i, (_, tm2)) =>
   287                              let val ctm2 = Thm.cterm_of s tm2
   287                              let val ctm2 = Thm.global_cterm_of s tm2
   288                              in (Thm.cterm_of s (Var(i, Thm.typ_of_cterm ctm2)), ctm2)
   288                              in (Thm.global_cterm_of s (Var(i, Thm.typ_of_cterm ctm2)), ctm2)
   289                              end)
   289                              end)
   290       fun certify s (ty_theta,tm_theta) =
   290       fun certify s (ty_theta,tm_theta) =
   291         (cty_theta s (Vartab.dest ty_theta),
   291         (cty_theta s (Vartab.dest ty_theta),
   292          ctm_theta s (Vartab.dest tm_theta))
   292          ctm_theta s (Vartab.dest tm_theta))
   293 in
   293 in
   299        val theta = Pattern.match thy (P,P') (Vartab.empty, Vartab.empty);
   299        val theta = Pattern.match thy (P,P') (Vartab.empty, Vartab.empty);
   300        val allI2 = Drule.instantiate_normalize (certify thy theta) allI
   300        val allI2 = Drule.instantiate_normalize (certify thy theta) allI
   301        val thm = Thm.implies_elim allI2 gth
   301        val thm = Thm.implies_elim allI2 gth
   302        val tp $ (A $ Abs(_,_,M)) = Thm.prop_of thm
   302        val tp $ (A $ Abs(_,_,M)) = Thm.prop_of thm
   303        val prop' = tp $ (A $ Abs(x,ty,M))
   303        val prop' = tp $ (A $ Abs(x,ty,M))
   304    in ALPHA thm (Thm.cterm_of thy prop')
   304    in ALPHA thm (Thm.global_cterm_of thy prop')
   305    end
   305    end
   306 end;
   306 end;
   307 
   307 
   308 val GENL = fold_rev GEN;
   308 val GENL = fold_rev GEN;
   309 
   309 
   310 fun GEN_ALL thm =
   310 fun GEN_ALL thm =
   311    let val thy = Thm.theory_of_thm thm
   311    let val thy = Thm.theory_of_thm thm
   312        val prop = Thm.prop_of thm
   312        val prop = Thm.prop_of thm
   313        val tycheck = Thm.cterm_of thy
   313        val tycheck = Thm.global_cterm_of thy
   314        val vlist = map tycheck (Misc_Legacy.add_term_vars (prop, []))
   314        val vlist = map tycheck (Misc_Legacy.add_term_vars (prop, []))
   315   in GENL vlist thm
   315   in GENL vlist thm
   316   end;
   316   end;
   317 
   317 
   318 
   318 
   341   let
   341   let
   342     val lam = #2 (Dcterm.dest_comb (Dcterm.drop_prop (cconcl exth)))
   342     val lam = #2 (Dcterm.dest_comb (Dcterm.drop_prop (cconcl exth)))
   343     val redex = Dcterm.capply lam fvar
   343     val redex = Dcterm.capply lam fvar
   344     val thy = Thm.theory_of_cterm redex
   344     val thy = Thm.theory_of_cterm redex
   345     val t$u = Thm.term_of redex
   345     val t$u = Thm.term_of redex
   346     val residue = Thm.cterm_of thy (Term.betapply (t, u))
   346     val residue = Thm.global_cterm_of thy (Term.betapply (t, u))
   347   in
   347   in
   348     GEN fvar (DISCH residue fact) RS (exth RS Thms.choose_thm)
   348     GEN fvar (DISCH residue fact) RS (exth RS Thms.choose_thm)
   349       handle THM (msg, _, _) => raise RULES_ERR "CHOOSE" msg
   349       handle THM (msg, _, _) => raise RULES_ERR "CHOOSE" msg
   350   end;
   350   end;
   351 
   351 
   354       val [P,x] = Misc_Legacy.term_vars prop
   354       val [P,x] = Misc_Legacy.term_vars prop
   355 in
   355 in
   356 fun EXISTS (template,witness) thm =
   356 fun EXISTS (template,witness) thm =
   357    let val thy = Thm.theory_of_thm thm
   357    let val thy = Thm.theory_of_thm thm
   358        val prop = Thm.prop_of thm
   358        val prop = Thm.prop_of thm
   359        val P' = Thm.cterm_of thy P
   359        val P' = Thm.global_cterm_of thy P
   360        val x' = Thm.cterm_of thy x
   360        val x' = Thm.global_cterm_of thy x
   361        val abstr = #2 (Dcterm.dest_comb template)
   361        val abstr = #2 (Dcterm.dest_comb template)
   362    in
   362    in
   363    thm RS (cterm_instantiate[(P',abstr), (x',witness)] exI)
   363    thm RS (cterm_instantiate[(P',abstr), (x',witness)] exI)
   364      handle THM (msg, _, _) => raise RULES_ERR "EXISTS" msg
   364      handle THM (msg, _, _) => raise RULES_ERR "EXISTS" msg
   365    end
   365    end
   387  *---------------------------------------------------------------------------*)
   387  *---------------------------------------------------------------------------*)
   388 (* Could be improved, but needs "subst_free" for certified terms *)
   388 (* Could be improved, but needs "subst_free" for certified terms *)
   389 
   389 
   390 fun IT_EXISTS blist th =
   390 fun IT_EXISTS blist th =
   391    let val thy = Thm.theory_of_thm th
   391    let val thy = Thm.theory_of_thm th
   392        val tych = Thm.cterm_of thy
   392        val tych = Thm.global_cterm_of thy
   393        val blist' = map (apply2 Thm.term_of) blist
   393        val blist' = map (apply2 Thm.term_of) blist
   394        fun ex v M  = Thm.cterm_of thy (USyntax.mk_exists{Bvar=v,Body = M})
   394        fun ex v M  = Thm.global_cterm_of thy (USyntax.mk_exists{Bvar=v,Body = M})
   395 
   395 
   396   in
   396   in
   397   fold_rev (fn (b as (r1,r2)) => fn thm =>
   397   fold_rev (fn (b as (r1,r2)) => fn thm =>
   398         EXISTS(ex r2 (subst_free [b]
   398         EXISTS(ex r2 (subst_free [b]
   399                    (HOLogic.dest_Trueprop(Thm.prop_of thm))), tych r1)
   399                    (HOLogic.dest_Trueprop(Thm.prop_of thm))), tych r1)
   509               | Utils.ERR _ => get (rst, n+1, L);
   509               | Utils.ERR _ => get (rst, n+1, L);
   510 
   510 
   511 (* Note: Thm.rename_params_rule counts from 1, not 0 *)
   511 (* Note: Thm.rename_params_rule counts from 1, not 0 *)
   512 fun rename thm =
   512 fun rename thm =
   513   let val thy = Thm.theory_of_thm thm
   513   let val thy = Thm.theory_of_thm thm
   514       val tych = Thm.cterm_of thy
   514       val tych = Thm.global_cterm_of thy
   515       val ants = Logic.strip_imp_prems (Thm.prop_of thm)
   515       val ants = Logic.strip_imp_prems (Thm.prop_of thm)
   516       val news = get (ants,1,[])
   516       val news = get (ants,1,[])
   517   in
   517   in
   518   fold Thm.rename_params_rule news thm
   518   fold Thm.rename_params_rule news thm
   519   end;
   519   end;
   661              val dummy = print_thms ctxt "cntxt:" cntxt
   661              val dummy = print_thms ctxt "cntxt:" cntxt
   662              val dummy = say "cong rule:"
   662              val dummy = say "cong rule:"
   663              val dummy = say (Display.string_of_thm ctxt thm)
   663              val dummy = say (Display.string_of_thm ctxt thm)
   664              (* Unquantified eliminate *)
   664              (* Unquantified eliminate *)
   665              fun uq_eliminate (thm,imp,thy) =
   665              fun uq_eliminate (thm,imp,thy) =
   666                  let val tych = Thm.cterm_of thy
   666                  let val tych = Thm.global_cterm_of thy
   667                      val dummy = print_cterm ctxt "To eliminate:" (tych imp)
   667                      val dummy = print_cterm ctxt "To eliminate:" (tych imp)
   668                      val ants = map tych (Logic.strip_imp_prems imp)
   668                      val ants = map tych (Logic.strip_imp_prems imp)
   669                      val eq = Logic.strip_imp_concl imp
   669                      val eq = Logic.strip_imp_concl imp
   670                      val lhs = tych(get_lhs eq)
   670                      val lhs = tych(get_lhs eq)
   671                      val ctxt' = Simplifier.add_prems (map ASSUME ants) ctxt
   671                      val ctxt' = Simplifier.add_prems (map ASSUME ants) ctxt
   681               let val ((vstrl, _, used'), args) = dest_pbeta_redex used lhs_eq (length vlist)
   681               let val ((vstrl, _, used'), args) = dest_pbeta_redex used lhs_eq (length vlist)
   682                   val dummy = forall (op aconv) (ListPair.zip (vlist, args))
   682                   val dummy = forall (op aconv) (ListPair.zip (vlist, args))
   683                     orelse error "assertion failed in CONTEXT_REWRITE_RULE"
   683                     orelse error "assertion failed in CONTEXT_REWRITE_RULE"
   684                   val imp_body1 = subst_free (ListPair.zip (args, vstrl))
   684                   val imp_body1 = subst_free (ListPair.zip (args, vstrl))
   685                                              imp_body
   685                                              imp_body
   686                   val tych = Thm.cterm_of thy
   686                   val tych = Thm.global_cterm_of thy
   687                   val ants1 = map tych (Logic.strip_imp_prems imp_body1)
   687                   val ants1 = map tych (Logic.strip_imp_prems imp_body1)
   688                   val eq1 = Logic.strip_imp_concl imp_body1
   688                   val eq1 = Logic.strip_imp_concl imp_body1
   689                   val Q = get_lhs eq1
   689                   val Q = get_lhs eq1
   690                   val QeqQ1 = pbeta_reduce (tych Q)
   690                   val QeqQ1 = pbeta_reduce (tych Q)
   691                   val Q1 = #2(Dcterm.dest_eq(cconcl QeqQ1))
   691                   val Q1 = #2(Dcterm.dest_eq(cconcl QeqQ1))
   710               let val (vlist, imp_body, used') = strip_all used imp
   710               let val (vlist, imp_body, used') = strip_all used imp
   711                   val (ants,Q) = dest_impl imp_body
   711                   val (ants,Q) = dest_impl imp_body
   712               in if (pbeta_redex Q) (length vlist)
   712               in if (pbeta_redex Q) (length vlist)
   713                  then pq_eliminate (thm,thy,vlist,imp_body,Q)
   713                  then pq_eliminate (thm,thy,vlist,imp_body,Q)
   714                  else
   714                  else
   715                  let val tych = Thm.cterm_of thy
   715                  let val tych = Thm.global_cterm_of thy
   716                      val ants1 = map tych ants
   716                      val ants1 = map tych ants
   717                      val ctxt' = Simplifier.add_prems (map ASSUME ants1) ctxt
   717                      val ctxt' = Simplifier.add_prems (map ASSUME ants1) ctxt
   718                      val Q_eeq_Q1 = Raw_Simplifier.rewrite_cterm
   718                      val Q_eeq_Q1 = Raw_Simplifier.rewrite_cterm
   719                         (false,true,false) (prover used') ctxt' (tych Q)
   719                         (false,true,false) (prover used') ctxt' (tych Q)
   720                       handle Utils.ERR _ => Thm.reflexive (tych Q)
   720                       handle Utils.ERR _ => Thm.reflexive (tych Q)
   761               val rcontext = rev cntxt
   761               val rcontext = rev cntxt
   762               val cncl = HOLogic.dest_Trueprop o Thm.prop_of
   762               val cncl = HOLogic.dest_Trueprop o Thm.prop_of
   763               val antl = case rcontext of [] => []
   763               val antl = case rcontext of [] => []
   764                          | _   => [USyntax.list_mk_conj(map cncl rcontext)]
   764                          | _   => [USyntax.list_mk_conj(map cncl rcontext)]
   765               val TC = genl(USyntax.list_mk_imp(antl, A))
   765               val TC = genl(USyntax.list_mk_imp(antl, A))
   766               val dummy = print_cterm ctxt "func:" (Thm.cterm_of thy func)
   766               val dummy = print_cterm ctxt "func:" (Thm.global_cterm_of thy func)
   767               val dummy = print_cterm ctxt "TC:" (Thm.cterm_of thy (HOLogic.mk_Trueprop TC))
   767               val dummy = print_cterm ctxt "TC:" (Thm.global_cterm_of thy (HOLogic.mk_Trueprop TC))
   768               val dummy = tc_list := (TC :: !tc_list)
   768               val dummy = tc_list := (TC :: !tc_list)
   769               val nestedp = is_some (USyntax.find_term is_func TC)
   769               val nestedp = is_some (USyntax.find_term is_func TC)
   770               val dummy = if nestedp then say "nested" else say "not_nested"
   770               val dummy = if nestedp then say "nested" else say "not_nested"
   771               val th' = if nestedp then raise RULES_ERR "solver" "nested function"
   771               val th' = if nestedp then raise RULES_ERR "solver" "nested function"
   772                         else let val cTC = Thm.cterm_of thy
   772                         else let val cTC = Thm.global_cterm_of thy
   773                                               (HOLogic.mk_Trueprop TC)
   773                                               (HOLogic.mk_Trueprop TC)
   774                              in case rcontext of
   774                              in case rcontext of
   775                                 [] => SPEC_ALL(ASSUME cTC)
   775                                 [] => SPEC_ALL(ASSUME cTC)
   776                                | _ => MP (SPEC_ALL (ASSUME cTC))
   776                                | _ => MP (SPEC_ALL (ASSUME cTC))
   777                                          (LIST_CONJ rcontext)
   777                                          (LIST_CONJ rcontext)