QuotMain.thy
changeset 301 40bb0c4718a6
parent 300 c6a9b4e4d548
child 302 a840c232e04e
equal deleted inserted replaced
300:c6a9b4e4d548 301:40bb0c4718a6
   157 use "quotient.ML"
   157 use "quotient.ML"
   158 
   158 
   159 
   159 
   160 (* lifting of constants *)
   160 (* lifting of constants *)
   161 use "quotient_def.ML"
   161 use "quotient_def.ML"
   162 
       
   163 
       
   164 text {* FIXME: auxiliary function *}
       
   165 ML {*
       
   166 val no_vars = Thm.rule_attribute (fn context => fn th =>
       
   167   let
       
   168     val ctxt = Variable.set_body false (Context.proof_of context);
       
   169     val ((_, [th']), _) = Variable.import true [th] ctxt;
       
   170   in th' end);
       
   171 *}
       
   172 
   162 
   173 section {* ATOMIZE *}
   163 section {* ATOMIZE *}
   174 
   164 
   175 lemma atomize_eqv[atomize]: 
   165 lemma atomize_eqv[atomize]: 
   176   shows "(Trueprop A \<equiv> Trueprop B) \<equiv> (A \<equiv> B)" 
   166   shows "(Trueprop A \<equiv> Trueprop B) \<equiv> (A \<equiv> B)" 
   338       else Const (@{const_name "op ="}, ty) $ (my_reg lthy rel rty t)
   328       else Const (@{const_name "op ="}, ty) $ (my_reg lthy rel rty t)
   339   | t1 $ t2 => (my_reg lthy rel rty t1) $ (my_reg lthy rel rty t2)
   329   | t1 $ t2 => (my_reg lthy rel rty t1) $ (my_reg lthy rel rty t2)
   340   | _ => trm
   330   | _ => trm
   341 *}
   331 *}
   342 
   332 
       
   333 (* For polymorphic types we need to find the type of the Relation term. *)
       
   334 (* TODO: we assume that the relation is a Constant. Is this always true? *)
   343 ML {*
   335 ML {*
   344 fun my_reg_inst lthy rel rty trm =
   336 fun my_reg_inst lthy rel rty trm =
   345   case rel of
   337   case rel of
   346     Const (n, _) => Syntax.check_term lthy
   338     Const (n, _) => Syntax.check_term lthy
   347       (my_reg lthy (Const (n, dummyT)) (Logic.varifyT rty) trm)
   339       (my_reg lthy (Const (n, dummyT)) (Logic.varifyT rty) trm)
   348 *}
   340 *}
   349 
   341 
   350 (*
   342 (*
   351 ML {*
   343 ML {*
   352   text {*val r = term_of @{cpat "R::?'a list \<Rightarrow> ?'a list \<Rightarrow>bool"};*}
   344   text {*val r = term_of @{cpat "R::?'a list \<Rightarrow> ?'a list \<Rightarrow> bool"};*}
   353   val r = Free ("R", dummyT);
   345   val r = Free ("R", dummyT);
   354   val t = (my_reg @{context} r @{typ "'a list"} @{term "\<forall>(x::'b list). P x"});
   346   val t = (my_reg @{context} r @{typ "'a list"} @{term "\<forall>(x::'b list). P x"});
   355   val t2 = Syntax.check_term @{context} t;
   347   val t2 = Syntax.check_term @{context} t;
   356   Toplevel.program (fn () => cterm_of @{theory} t2)
   348   Toplevel.program (fn () => cterm_of @{theory} t2)
   357 *}*)
   349 *}*)
   370 
   362 
   371 lemma implication_twice: 
   363 lemma implication_twice: 
   372   "(c \<longrightarrow> a) \<Longrightarrow> (a \<Longrightarrow> b \<longrightarrow> d) \<Longrightarrow> (a \<longrightarrow> b) \<longrightarrow> (c \<longrightarrow> d)"
   364   "(c \<longrightarrow> a) \<Longrightarrow> (a \<Longrightarrow> b \<longrightarrow> d) \<Longrightarrow> (a \<longrightarrow> b) \<longrightarrow> (c \<longrightarrow> d)"
   373 by auto
   365 by auto
   374 
   366 
   375 (*lemma equality_twice: "a = c \<Longrightarrow> b = d \<Longrightarrow> (a = b \<longrightarrow> c = d)"
   367 (*lemma equality_twice:
       
   368   "a = c \<Longrightarrow> b = d \<Longrightarrow> (a = b \<longrightarrow> c = d)"
   376 by auto*)
   369 by auto*)
   377 
   370 
   378 ML {*
   371 ML {*
   379 fun regularize thm rty rel rel_eqv rel_refl lthy =
   372 fun regularize thm rty rel rel_eqv rel_refl lthy =
   380   let
   373   let
   381     val goal = build_regularize_goal thm rty rel lthy;
   374     val goal = build_regularize_goal thm rty rel lthy;
   382     fun tac ctxt =
   375     fun tac ctxt =
   383       (ObjectLogic.full_atomize_tac) THEN'
   376       (ObjectLogic.full_atomize_tac) THEN'
   384      REPEAT_ALL_NEW (FIRST' [
   377       REPEAT_ALL_NEW (FIRST' [
   385       rtac rel_refl,
   378         rtac rel_refl,
   386       atac,
   379         atac,
   387       rtac @{thm universal_twice},
   380         rtac @{thm universal_twice},
   388       (rtac @{thm impI} THEN' atac),
   381         (rtac @{thm impI} THEN' atac),
   389       rtac @{thm implication_twice},
   382         rtac @{thm implication_twice},
   390       (*rtac @{thm equality_twice},*)
   383         EqSubst.eqsubst_tac ctxt [0]
   391       EqSubst.eqsubst_tac ctxt [0]
   384           [(@{thm equiv_res_forall} OF [rel_eqv]),
   392         [(@{thm equiv_res_forall} OF [rel_eqv]),
   385            (@{thm equiv_res_exists} OF [rel_eqv])],
   393          (@{thm equiv_res_exists} OF [rel_eqv])],
   386         (rtac @{thm impI} THEN' (asm_full_simp_tac HOL_ss) THEN' rtac rel_refl),
   394       (rtac @{thm impI} THEN' (asm_full_simp_tac HOL_ss) THEN' rtac rel_refl),
   387         (rtac @{thm RIGHT_RES_FORALL_REGULAR})
   395       (rtac @{thm RIGHT_RES_FORALL_REGULAR})
   388       ]);
   396      ]);
   389     val cthm = Goal.prove lthy [] [] goal
   397     val cthm = Goal.prove lthy [] [] goal 
   390       (fn {context, ...} => tac context 1);
   398       (fn {context,...} => tac context 1);
       
   399   in
   391   in
   400     cthm OF [thm]
   392     cthm OF [thm]
   401   end
   393   end
   402 *}
   394 *}
   403 
   395 
   404 section {* RepAbs injection *}
   396 section {* RepAbs injection *}
       
   397 (*
       
   398 
       
   399 Injecting RepAbs means:
       
   400 
       
   401   For abstractions:
       
   402   * If the type of the abstraction doesn't need lifting we recurse.
       
   403   * If it does we add RepAbs around the whole term and check if the
       
   404     variable needs lifting.
       
   405     * If it doesn't then we recurse
       
   406     * If it does we recurse and put 'RepAbs' around all occurences
       
   407       of the variable in the obtained subterm.
       
   408   For applications:
       
   409   * If the term is 'Respects' applied to anything we leave it unchanged
       
   410   * If the term needs lifting and the head is a constant that we know
       
   411     how to lift, we put a RepAbs and recurse
       
   412   * If the term needs lifting and the head is a free applied to subterms
       
   413     (if it is not applied we treated it in Abs branch) then we
       
   414     put RepAbs and recurse
       
   415   * Otherwise just recurse.
       
   416 
       
   417 The injection is done in the following phases:
       
   418  1) build_repabs_term inserts rep-abs pairs in the term
       
   419  2) we prove the equality between the original theorem and this one
       
   420  3) we use Pure.equal_elim_rule1 to get the new theorem.
       
   421 
       
   422 To prove that the old theorem implies the new one, we first
       
   423 atomize it and then try:
       
   424 
       
   425  1) theorems 'trans2' from the QUOT_TYPE
       
   426  2) remove lambdas from both sides (LAMBDA_RES_TAC)
       
   427  3) remove Ball/Bex
       
   428  4) use RSP theorems
       
   429  5) remove rep_abs from right side
       
   430  6) reflexivity
       
   431  7) split applications of lifted type (apply_rsp)
       
   432  8) split applications of non-lifted type (cong_tac)
       
   433  9) apply extentionality
       
   434 10) relation reflexive
       
   435 11) assumption
       
   436 12) proving obvious higher order equalities by simplifying fun_rel
       
   437     (not sure if still needed?)
       
   438 13) unfolding lambda on one side
       
   439 14) simplifying (= ===> =) for simpler respectfullness
       
   440 
       
   441 *)
       
   442 
   405 
   443 
   406 (* Needed to have a meta-equality *)
   444 (* Needed to have a meta-equality *)
   407 lemma id_def_sym: "(\<lambda>x. x) \<equiv> id"
   445 lemma id_def_sym: "(\<lambda>x. x) \<equiv> id"
   408 by (simp add: id_def)
   446 by (simp add: id_def)
   409 
   447 
   487 in
   525 in
   488   if (Sign.typ_instance thy (ty, rty))
   526   if (Sign.typ_instance thy (ty, rty))
   489   then (get_const flag (ty, (exchange_ty lthy rty qty ty)))
   527   then (get_const flag (ty, (exchange_ty lthy rty qty ty)))
   490   else (case ty of
   528   else (case ty of
   491           TFree _ => (mk_identity ty, (ty, ty))
   529           TFree _ => (mk_identity ty, (ty, ty))
   492         | Type (_, []) => (mk_identity ty, (ty, ty)) 
   530         | Type (_, []) => (mk_identity ty, (ty, ty))
   493         | Type ("fun" , [ty1, ty2]) => 
   531         | Type ("fun" , [ty1, ty2]) =>
   494                  get_fun_fun [get_fun_noexchange (negF flag) (rty,qty) lthy ty1, get_fun_noexchange flag (rty,qty) lthy ty2]
   532                  get_fun_fun [get_fun_noexchange (negF flag) (rty, qty) lthy ty1,
       
   533                  get_fun_noexchange flag (rty, qty) lthy ty2]
   495         | Type (s, tys) => get_fun_aux s (map (get_fun_noexchange flag (rty, qty) lthy) tys)
   534         | Type (s, tys) => get_fun_aux s (map (get_fun_noexchange flag (rty, qty) lthy) tys)
   496         | _ => raise ERROR ("no type variables"))
   535         | _ => raise ERROR ("no type variables"))
   497 end
   536 end
   498 fun get_fun_noex flag (rty, qty) lthy ty =
   537 fun get_fun_noex flag (rty, qty) lthy ty =
   499   fst (get_fun_noexchange flag (rty, qty) lthy ty)
   538   fst (get_fun_noexchange flag (rty, qty) lthy ty)
   580     | SOME th => cprem_of th 1
   619     | SOME th => cprem_of th 1
   581     val tac = (EqSubst.eqsubst_tac ctxt [0] thms 1) THEN simp_tac HOL_ss 1
   620     val tac = (EqSubst.eqsubst_tac ctxt [0] thms 1) THEN simp_tac HOL_ss 1
   582     val cgoal = cterm_of (ProofContext.theory_of ctxt) (Logic.mk_equals (term_of (Thm.cprop_of thm), term_of a'))
   621     val cgoal = cterm_of (ProofContext.theory_of ctxt) (Logic.mk_equals (term_of (Thm.cprop_of thm), term_of a'))
   583     val rt = Toplevel.program (fn () => Goal.prove_internal [] cgoal (fn _ => tac));
   622     val rt = Toplevel.program (fn () => Goal.prove_internal [] cgoal (fn _ => tac));
   584   in
   623   in
   585     @{thm Pure.equal_elim_rule1} OF [rt,thm]
   624     @{thm Pure.equal_elim_rule1} OF [rt, thm]
   586   end
   625   end
   587 *}
   626 *}
   588 
   627 
   589 ML {*
   628 ML {*
   590   fun repeat_eqsubst_thm ctxt thms thm =
   629   fun repeat_eqsubst_thm ctxt thms thm =
   591     repeat_eqsubst_thm ctxt thms (eqsubst_thm ctxt thms thm)
   630     repeat_eqsubst_thm ctxt thms (eqsubst_thm ctxt thms thm)
   592     handle _ => thm
   631     handle _ => thm
   593 *}
   632 *}
   594 
   633 
   595 ML {*
   634 ML {*
   596 fun build_repabs_term lthy thm constructors rty qty =
   635 fun build_repabs_term lthy thm consts rty qty =
   597   let
   636   let
   598     val rty = Logic.varifyT rty;
   637     val rty = Logic.varifyT rty;
   599     val qty = Logic.varifyT qty;
   638     val qty = Logic.varifyT qty;
   600 
   639 
   601   fun mk_abs tm =
   640   fun mk_abs tm =
   605   fun mk_repabs tm =
   644   fun mk_repabs tm =
   606     let
   645     let
   607       val ty = fastype_of tm
   646       val ty = fastype_of tm
   608     in Syntax.check_term lthy ((get_fun_new repF (rty, qty) lthy ty) $ (mk_abs tm)) end
   647     in Syntax.check_term lthy ((get_fun_new repF (rty, qty) lthy ty) $ (mk_abs tm)) end
   609 
   648 
   610     fun is_constructor (Const (x, _)) = member (op =) constructors x
   649     fun is_lifted_const (Const (x, _)) = member (op =) consts x
   611       | is_constructor _ = false;
   650       | is_lifted_const _ = false;
   612 
   651 
   613     fun build_aux lthy tm =
   652     fun build_aux lthy tm =
   614       case tm of
   653       case tm of
   615       Abs (a as (_, vty, _)) =>
   654         Abs (a as (_, vty, _)) =>
   616       let
       
   617         val (vs, t) = Term.dest_abs a;
       
   618         val v = Free(vs, vty);
       
   619         val t' = lambda v (build_aux lthy t)
       
   620       in
       
   621       if (not (needs_lift rty (fastype_of tm))) then t'
       
   622       else mk_repabs (
       
   623         if not (needs_lift rty vty) then t'
       
   624         else
       
   625           let
   655           let
   626             val v' = mk_repabs v;
   656             val (vs, t) = Term.dest_abs a;
   627             val t1 = Envir.beta_norm (t' $ v')
   657             val v = Free(vs, vty);
       
   658             val t' = lambda v (build_aux lthy t)
   628           in
   659           in
   629             lambda v t1
   660             if (not (needs_lift rty (fastype_of tm))) then t'
       
   661             else mk_repabs (
       
   662               if not (needs_lift rty vty) then t'
       
   663               else
       
   664                 let
       
   665                   val v' = mk_repabs v;
       
   666                   (* TODO: I believe this is not needed any more *)
       
   667                   val t1 = Envir.beta_norm (t' $ v')
       
   668                 in
       
   669                   lambda v t1
       
   670                 end)
   630           end
   671           end
   631       )
   672       | x =>
   632       end
   673         case Term.strip_comb tm of
   633     | x =>
   674           (Const(@{const_name Respects}, _), _) => tm
   634       let
   675         | (opp, tms0) =>
   635         val (opp, tms0) = Term.strip_comb tm
   676           let
   636         val tms = map (build_aux lthy) tms0
   677             val tms = map (build_aux lthy) tms0
   637         val ty = fastype_of tm
   678             val ty = fastype_of tm
   638       in
   679           in
   639         if (((fst (Term.dest_Const opp)) = @{const_name Respects}) handle _ => false)
   680             if (is_lifted_const opp andalso needs_lift rty ty) then
   640           then (list_comb (opp, (hd tms0) :: (tl tms)))
   681             mk_repabs (list_comb (opp, tms))
   641       else if (is_constructor opp andalso needs_lift rty ty) then
   682             else if ((Term.is_Free opp) andalso (length tms > 0) andalso (needs_lift rty ty)) then
   642           mk_repabs (list_comb (opp,tms))
   683               mk_repabs (list_comb (opp, tms))
   643         else if ((Term.is_Free opp) andalso (length tms > 0) andalso (needs_lift rty ty)) then
   684             else if tms = [] then opp
   644           mk_repabs(list_comb(opp,tms))
   685             else list_comb(opp, tms)
   645         else if tms = [] then opp
   686           end
   646         else list_comb(opp, tms)
       
   647       end
       
   648   in
   687   in
   649     repeat_eqsubst_prop lthy @{thms id_def_sym}
   688     repeat_eqsubst_prop lthy @{thms id_def_sym}
   650       (build_aux lthy (Thm.prop_of thm))
   689       (build_aux lthy (Thm.prop_of thm))
   651   end
   690   end
   652 *}
   691 *}
   673 fun quotient_tac quot_thm =
   712 fun quotient_tac quot_thm =
   674   REPEAT_ALL_NEW (FIRST' [
   713   REPEAT_ALL_NEW (FIRST' [
   675     rtac @{thm FUN_QUOTIENT},
   714     rtac @{thm FUN_QUOTIENT},
   676     rtac quot_thm,
   715     rtac quot_thm,
   677     rtac @{thm IDENTITY_QUOTIENT},
   716     rtac @{thm IDENTITY_QUOTIENT},
   678     (fn i => CHANGED (simp_tac (HOL_ss addsimps @{thms FUN_MAP_I}) i) THEN rtac @{thm IDENTITY_QUOTIENT} i)
   717     (
       
   718       fn i => CHANGED (simp_tac (HOL_ss addsimps @{thms FUN_MAP_I}) i) THEN
       
   719       rtac @{thm IDENTITY_QUOTIENT} i
       
   720     )
   679   ])
   721   ])
   680 *}
   722 *}
   681 
   723 
   682 ML {*
   724 ML {*
   683 fun LAMBDA_RES_TAC ctxt i st =
   725 fun LAMBDA_RES_TAC ctxt i st =
   684   (case (term_of o #concl o fst) (Subgoal.focus ctxt i st) of
   726   (case (term_of o #concl o fst) (Subgoal.focus ctxt i st) of
   685     (_ $ (_ $ (Abs(_,_,_))$(Abs(_,_,_)))) =>
   727     (_ $ (_ $ (Abs(_, _, _))$(Abs(_, _, _)))) =>
   686       (EqSubst.eqsubst_tac ctxt [0] @{thms FUN_REL.simps}) THEN'
   728       (EqSubst.eqsubst_tac ctxt [0] @{thms FUN_REL.simps}) THEN'
   687       (rtac @{thm allI}) THEN' (rtac @{thm allI}) THEN' (rtac @{thm impI})
   729       (rtac @{thm allI}) THEN' (rtac @{thm allI}) THEN' (rtac @{thm impI})
   688   | _ => fn _ => no_tac) i st
   730   | _ => fn _ => no_tac) i st
   689 *}
   731 *}
   690 
   732 
   691 ML {*
   733 ML {*
   692 fun WEAK_LAMBDA_RES_TAC ctxt i st =
   734 fun WEAK_LAMBDA_RES_TAC ctxt i st =
   693   (case (term_of o #concl o fst) (Subgoal.focus ctxt i st) of
   735   (case (term_of o #concl o fst) (Subgoal.focus ctxt i st) of
   694     (_ $ (_ $ _$(Abs(_,_,_)))) =>
   736     (_ $ (_ $ _ $ (Abs(_, _, _)))) =>
   695       (EqSubst.eqsubst_tac ctxt [0] @{thms FUN_REL.simps}) THEN'
   737       (EqSubst.eqsubst_tac ctxt [0] @{thms FUN_REL.simps}) THEN'
   696       (rtac @{thm allI}) THEN' (rtac @{thm allI}) THEN' (rtac @{thm impI})
   738       (rtac @{thm allI}) THEN' (rtac @{thm allI}) THEN' (rtac @{thm impI})
   697   | (_ $ (_ $ (Abs(_,_,_))$_)) =>
   739   | (_ $ (_ $ (Abs(_, _, _)) $ _)) =>
   698       (EqSubst.eqsubst_tac ctxt [0] @{thms FUN_REL.simps}) THEN'
   740       (EqSubst.eqsubst_tac ctxt [0] @{thms FUN_REL.simps}) THEN'
   699       (rtac @{thm allI}) THEN' (rtac @{thm allI}) THEN' (rtac @{thm impI})
   741       (rtac @{thm allI}) THEN' (rtac @{thm allI}) THEN' (rtac @{thm impI})
   700   | _ => fn _ => no_tac) i st
   742   | _ => fn _ => no_tac) i st
   701 *}
   743 *}
   702 
   744 
   713 end
   755 end
   714 handle _ => no_tac)
   756 handle _ => no_tac)
   715 *}
   757 *}
   716 
   758 
   717 ML {*
   759 ML {*
   718 val res_forall_rsp_tac = Subgoal.FOCUS (fn {concl, context = ctxt, ...} =>
   760 val ball_rsp_tac = Subgoal.FOCUS (fn {concl, context = ctxt, ...} =>
   719   let
   761   let
   720     val _ $ (_ $ (Const (@{const_name Ball}, _) $ _) $ (Const (@{const_name Ball}, _) $ _)) = term_of concl
   762     val _ $ (_ $ (Const (@{const_name Ball}, _) $ _) $
       
   763                  (Const (@{const_name Ball}, _) $ _)) = term_of concl
   721   in
   764   in
   722     ((simp_tac ((Simplifier.context ctxt HOL_ss) addsimps @{thms FUN_REL.simps}))
   765     ((simp_tac ((Simplifier.context ctxt HOL_ss) addsimps @{thms FUN_REL.simps}))
   723     THEN' rtac @{thm allI} THEN' rtac @{thm allI} THEN' rtac @{thm impI}
   766     THEN' rtac @{thm allI} THEN' rtac @{thm allI} THEN' rtac @{thm impI}
   724     THEN' instantiate_tac @{thm RES_FORALL_RSP} ctxt THEN'
   767     THEN' instantiate_tac @{thm RES_FORALL_RSP} ctxt THEN'
   725     (simp_tac ((Simplifier.context ctxt HOL_ss) addsimps @{thms FUN_REL.simps}))) 1
   768     (simp_tac ((Simplifier.context ctxt HOL_ss) addsimps @{thms FUN_REL.simps}))) 1
   727   handle _ => no_tac
   770   handle _ => no_tac
   728   )
   771   )
   729 *}
   772 *}
   730 
   773 
   731 ML {*
   774 ML {*
   732 val res_exists_rsp_tac = Subgoal.FOCUS (fn {concl, context = ctxt, ...} =>
   775 val bex_rsp_tac = Subgoal.FOCUS (fn {concl, context = ctxt, ...} =>
   733   let
   776   let
   734     val _ $ (_ $ (Const (@{const_name Bex}, _) $ _) $ (Const (@{const_name Bex}, _) $ _)) = term_of concl
   777     val _ $ (_ $ (Const (@{const_name Bex}, _) $ _) $
       
   778                  (Const (@{const_name Bex}, _) $ _)) = term_of concl
   735   in
   779   in
   736     ((simp_tac ((Simplifier.context ctxt HOL_ss) addsimps @{thms FUN_REL.simps}))
   780     ((simp_tac ((Simplifier.context ctxt HOL_ss) addsimps @{thms FUN_REL.simps}))
   737     THEN' rtac @{thm allI} THEN' rtac @{thm allI} THEN' rtac @{thm impI}
   781     THEN' rtac @{thm allI} THEN' rtac @{thm allI} THEN' rtac @{thm impI}
   738     THEN' instantiate_tac @{thm RES_EXISTS_RSP} ctxt THEN'
   782     THEN' instantiate_tac @{thm RES_EXISTS_RSP} ctxt THEN'
   739     (simp_tac ((Simplifier.context ctxt HOL_ss) addsimps @{thms FUN_REL.simps}))) 1
   783     (simp_tac ((Simplifier.context ctxt HOL_ss) addsimps @{thms FUN_REL.simps}))) 1
   743 *}
   787 *}
   744 
   788 
   745 ML {*
   789 ML {*
   746 fun r_mk_comb_tac ctxt rty quot_thm reflex_thm trans_thm rsp_thms =
   790 fun r_mk_comb_tac ctxt rty quot_thm reflex_thm trans_thm rsp_thms =
   747   (FIRST' [
   791   (FIRST' [
   748 (*    rtac @{thm FUN_QUOTIENT},
       
   749     rtac quot_thm,
       
   750     rtac @{thm IDENTITY_QUOTIENT},*)
       
   751     rtac trans_thm,
   792     rtac trans_thm,
   752     LAMBDA_RES_TAC ctxt,
   793     LAMBDA_RES_TAC ctxt,
   753     res_forall_rsp_tac ctxt,
   794     ball_rsp_tac ctxt,
   754     res_exists_rsp_tac ctxt,
   795     bex_rsp_tac ctxt,
   755     FIRST' (map rtac rsp_thms),
   796     FIRST' (map rtac rsp_thms),
   756     (instantiate_tac @{thm REP_ABS_RSP(1)} ctxt THEN' (RANGE [quotient_tac quot_thm])),
   797     (instantiate_tac @{thm REP_ABS_RSP(1)} ctxt THEN' (RANGE [quotient_tac quot_thm])),
   757     rtac refl,
   798     rtac refl,
   758 (*    rtac @{thm arg_cong2[of _ _ _ _ "op ="]},*)
   799 (*    rtac @{thm arg_cong2[of _ _ _ _ "op ="]},*)
   759     (APPLY_RSP_TAC rty ctxt THEN' (RANGE [quotient_tac quot_thm, quotient_tac quot_thm])),
   800     (APPLY_RSP_TAC rty ctxt THEN' (RANGE [quotient_tac quot_thm, quotient_tac quot_thm])),
   769     (fn i => CHANGED (asm_full_simp_tac (HOL_ss addsimps @{thms FUN_REL_EQ}) i))
   810     (fn i => CHANGED (asm_full_simp_tac (HOL_ss addsimps @{thms FUN_REL_EQ}) i))
   770     ])
   811     ])
   771 *}
   812 *}
   772 
   813 
   773 ML {*
   814 ML {*
   774 fun repabs lthy thm constructors rty qty quot_thm reflex_thm trans_thm rsp_thms =
   815 fun repabs lthy thm consts rty qty quot_thm reflex_thm trans_thm rsp_thms =
   775   let
   816   let
   776     val rt = build_repabs_term lthy thm constructors rty qty;
   817     val rt = build_repabs_term lthy thm consts rty qty;
   777     val rg = Logic.mk_equals ((Thm.prop_of thm), rt);
   818     val rg = Logic.mk_equals ((Thm.prop_of thm), rt);
   778     fun tac ctxt = (ObjectLogic.full_atomize_tac) THEN'
   819     fun tac ctxt = (ObjectLogic.full_atomize_tac) THEN'
   779       (REPEAT_ALL_NEW (r_mk_comb_tac ctxt rty quot_thm reflex_thm trans_thm rsp_thms));
   820       (REPEAT_ALL_NEW (r_mk_comb_tac ctxt rty quot_thm reflex_thm trans_thm rsp_thms));
   780     val cthm = Goal.prove lthy [] [] rg (fn x => tac (#context x) 1);
   821     val cthm = Goal.prove lthy [] [] rg (fn x => tac (#context x) 1);
   781   in
   822   in
   800   |> repeat_eqsubst_thm lthy @{thms FUN_MAP_I id_apply prod_fun_id map_id}
   841   |> repeat_eqsubst_thm lthy @{thms FUN_MAP_I id_apply prod_fun_id map_id}
   801 *}
   842 *}
   802 
   843 
   803 text {* expects atomized definition *}
   844 text {* expects atomized definition *}
   804 ML {*
   845 ML {*
   805   fun add_lower_defs_aux lthy thm =
   846 fun add_lower_defs_aux lthy thm =
   806     let
   847   let
   807       val e1 = @{thm fun_cong} OF [thm];
   848     val e1 = @{thm fun_cong} OF [thm];
   808       val f = eqsubst_thm lthy @{thms fun_map.simps} e1;
   849     val f = eqsubst_thm lthy @{thms fun_map.simps} e1;
   809       val g = simp_ids lthy f
   850     val g = simp_ids lthy f
   810     in
   851   in
   811       (simp_ids lthy thm) :: (add_lower_defs_aux lthy g)
   852     (simp_ids lthy thm) :: (add_lower_defs_aux lthy g)
   812     end
   853   end
   813     handle _ => [simp_ids lthy thm]
   854   handle _ => [simp_ids lthy thm]
   814 *}
   855 *}
   815 
   856 
   816 ML {*
   857 ML {*
   817 fun add_lower_defs lthy def =
   858 fun add_lower_defs lthy def =
   818   let
   859   let
   823     map Thm.varifyT defs_all
   864     map Thm.varifyT defs_all
   824   end
   865   end
   825 *}
   866 *}
   826 
   867 
   827 ML {*
   868 ML {*
   828   fun findabs_all rty tm =
   869 fun findabs_all rty tm =
   829     case tm of
   870   case tm of
   830       Abs(_, T, b) =>
   871     Abs(_, T, b) =>
   831         let
   872       let
   832           val b' = subst_bound ((Free ("x", T)), b);
   873         val b' = subst_bound ((Free ("x", T)), b);
   833           val tys = findabs_all rty b'
   874         val tys = findabs_all rty b'
   834           val ty = fastype_of tm
   875         val ty = fastype_of tm
   835         in if needs_lift rty ty then (ty :: tys) else tys
   876       in if needs_lift rty ty then (ty :: tys) else tys
   836         end
   877       end
   837     | f $ a => (findabs_all rty f) @ (findabs_all rty a)
   878   | f $ a => (findabs_all rty f) @ (findabs_all rty a)
   838     | _ => [];
   879   | _ => [];
   839   fun findabs rty tm = distinct (op =) (findabs_all rty tm)
   880 fun findabs rty tm = distinct (op =) (findabs_all rty tm)
   840 *}
   881 *}
   841 
   882 
   842 
   883 
   843 ML {*
   884 ML {*
   844   fun findaps_all rty tm =
   885 fun findaps_all rty tm =
   845     case tm of
   886   case tm of
   846       Abs(_, T, b) =>
   887     Abs(_, T, b) =>
   847         findaps_all rty (subst_bound ((Free ("x", T)), b))
   888       findaps_all rty (subst_bound ((Free ("x", T)), b))
   848     | (f $ a) => (findaps_all rty f @ findaps_all rty a)
   889   | (f $ a) => (findaps_all rty f @ findaps_all rty a)
   849     | Free (_, (T as (Type ("fun", (_ :: _))))) => (if needs_lift rty T then [T] else [])
   890   | Free (_, (T as (Type ("fun", (_ :: _))))) =>
   850     | _ => [];
   891       (if needs_lift rty T then [T] else [])
   851   fun findaps rty tm = distinct (op =) (findaps_all rty tm)
   892   | _ => [];
       
   893 fun findaps rty tm = distinct (op =) (findaps_all rty tm)
   852 *}
   894 *}
   853 
   895 
   854 ML {*
   896 ML {*
   855 fun make_simp_prs_thm lthy quot_thm thm typ =
   897 fun make_simp_prs_thm lthy quot_thm thm typ =
   856   let
   898   let
   868     MetaSimplifier.rewrite_rule [@{thm eq_reflection} OF @{thms id_apply}] t
   910     MetaSimplifier.rewrite_rule [@{thm eq_reflection} OF @{thms id_apply}] t
   869   end
   911   end
   870 *}
   912 *}
   871 
   913 
   872 ML {*
   914 ML {*
   873   fun findallex_all rty qty tm =
   915 fun findallex_all rty qty tm =
   874     case tm of
   916   case tm of
   875       Const (@{const_name All}, T) $ (s as (Abs(_, _, b))) =>
   917     Const (@{const_name All}, T) $ (s as (Abs(_, _, b))) =>
   876         let
   918       let
   877           val (tya, tye) = findallex_all rty qty s
   919         val (tya, tye) = findallex_all rty qty s
   878         in if needs_lift rty T then
   920       in if needs_lift rty T then
   879           ((T :: tya), tye)
   921         ((T :: tya), tye)
   880         else (tya, tye) end
   922       else (tya, tye) end
   881     | Const (@{const_name Ex}, T) $ (s as (Abs(_, _, b))) =>
   923   | Const (@{const_name Ex}, T) $ (s as (Abs(_, _, b))) =>
   882         let
   924       let
   883           val (tya, tye) = findallex_all rty qty s
   925         val (tya, tye) = findallex_all rty qty s
   884         in if needs_lift rty T then
   926       in if needs_lift rty T then
   885           (tya, (T :: tye))
   927         (tya, (T :: tye))
   886         else (tya, tye) end
   928       else (tya, tye) end
   887     | Abs(_, T, b) =>
   929   | Abs(_, T, b) =>
   888         findallex_all rty qty (subst_bound ((Free ("x", T)), b))
   930       findallex_all rty qty (subst_bound ((Free ("x", T)), b))
   889     | f $ a =>
   931   | f $ a =>
   890         let
   932       let
   891           val (a1, e1) = findallex_all rty qty f;
   933         val (a1, e1) = findallex_all rty qty f;
   892           val (a2, e2) = findallex_all rty qty a;
   934         val (a2, e2) = findallex_all rty qty a;
   893         in (a1 @ a2, e1 @ e2) end
   935       in (a1 @ a2, e1 @ e2) end
   894     | _ => ([], []);
   936   | _ => ([], []);
   895 *}
   937 *}
   896 ML {*
   938 ML {*
   897   fun findallex lthy rty qty tm =
   939 fun findallex lthy rty qty tm =
   898     let
   940   let
   899       val (a, e) = findallex_all rty qty tm;
   941     val (a, e) = findallex_all rty qty tm;
   900       val (ad, ed) = (map domain_type a, map domain_type e);
   942     val (ad, ed) = (map domain_type a, map domain_type e);
   901       val (au, eu) = (distinct (op =) ad, distinct (op =) ed);
   943     val (au, eu) = (distinct (op =) ad, distinct (op =) ed);
   902       val (rty, qty) = (Logic.varifyT rty, Logic.varifyT qty)
   944     val (rty, qty) = (Logic.varifyT rty, Logic.varifyT qty)
   903     in
   945   in
   904       (map (exchange_ty lthy rty qty) au, map (exchange_ty lthy rty qty) eu)
   946     (map (exchange_ty lthy rty qty) au, map (exchange_ty lthy rty qty) eu)
   905     end
   947   end
   906 *}
   948 *}
   907 
   949 
   908 ML {*
   950 ML {*
   909 fun make_allex_prs_thm lthy quot_thm thm typ =
   951 fun make_allex_prs_thm lthy quot_thm thm typ =
   910   let
   952   let
   916     val tac =
   958     val tac =
   917       (compose_tac (false, lpi, 1)) THEN_ALL_NEW
   959       (compose_tac (false, lpi, 1)) THEN_ALL_NEW
   918       (quotient_tac quot_thm);
   960       (quotient_tac quot_thm);
   919     val gc = Drule.strip_imp_concl (cprop_of lpi);
   961     val gc = Drule.strip_imp_concl (cprop_of lpi);
   920     val t = Goal.prove_internal [] gc (fn _ => tac 1)
   962     val t = Goal.prove_internal [] gc (fn _ => tac 1)
   921     val t_noid = MetaSimplifier.rewrite_rule [@{thm eq_reflection} OF @{thms id_apply}] t;
   963     val t_noid = MetaSimplifier.rewrite_rule
       
   964       [@{thm eq_reflection} OF @{thms id_apply}] t;
   922     val t_sym = @{thm "HOL.sym"} OF [t_noid];
   965     val t_sym = @{thm "HOL.sym"} OF [t_noid];
   923     val t_eq = @{thm "eq_reflection"} OF [t_sym]
   966     val t_eq = @{thm "eq_reflection"} OF [t_sym]
   924   in
   967   in
   925     t_eq
   968     t_eq
   926   end
   969   end
   927 *}
   970 *}
   928 
   971 
   929 ML {*
   972 ML {*
   930 fun applic_prs lthy rty qty absrep ty =
   973 fun applic_prs lthy rty qty absrep ty =
   931  let
   974   let
   932     val rty = Logic.varifyT rty;
   975     val rty = Logic.varifyT rty;
   933     val qty = Logic.varifyT qty;
   976     val qty = Logic.varifyT qty;
   934   fun absty ty =
   977     fun absty ty =
   935     exchange_ty lthy rty qty ty
   978       exchange_ty lthy rty qty ty
   936   fun mk_rep tm =
   979     fun mk_rep tm =
   937     let
   980       let
   938       val ty = exchange_ty lthy qty rty (fastype_of tm)
   981         val ty = exchange_ty lthy qty rty (fastype_of tm)
   939     in Syntax.check_term lthy ((get_fun_new repF (rty, qty) lthy ty) $ tm) end;
   982       in Syntax.check_term lthy ((get_fun_new repF (rty, qty) lthy ty) $ tm) end;
   940   fun mk_abs tm =
   983     fun mk_abs tm =
   941     let
   984       let
   942       val ty = fastype_of tm
   985         val ty = fastype_of tm
   943     in Syntax.check_term lthy ((get_fun_new absF (rty, qty) lthy ty) $ tm) end
   986       in Syntax.check_term lthy ((get_fun_new absF (rty, qty) lthy ty) $ tm) end
   944   val (l, ltl) = Term.strip_type ty;
   987     val (l, ltl) = Term.strip_type ty;
   945   val nl = map absty l;
   988     val nl = map absty l;
   946   val vs = map (fn _ => "x") l;
   989     val vs = map (fn _ => "x") l;
   947   val ((fname :: vfs), lthy') = Variable.variant_fixes ("f" :: vs) lthy;
   990     val ((fname :: vfs), lthy') = Variable.variant_fixes ("f" :: vs) lthy;
   948   val args = map Free (vfs ~~ nl);
   991     val args = map Free (vfs ~~ nl);
   949   val lhs = list_comb((Free (fname, nl ---> ltl)), args);
   992     val lhs = list_comb((Free (fname, nl ---> ltl)), args);
   950   val rargs = map mk_rep args;
   993     val rargs = map mk_rep args;
   951   val f = Free (fname, nl ---> ltl);
   994     val f = Free (fname, nl ---> ltl);
   952   val rhs = mk_abs (list_comb((mk_rep f), rargs));
   995     val rhs = mk_abs (list_comb((mk_rep f), rargs));
   953   val eq = Logic.mk_equals (rhs, lhs);
   996     val eq = Logic.mk_equals (rhs, lhs);
   954   val ceq = cterm_of (ProofContext.theory_of lthy') eq;
   997     val ceq = cterm_of (ProofContext.theory_of lthy') eq;
   955   val sctxt = (Simplifier.context lthy' HOL_ss) addsimps (absrep :: @{thms fun_map.simps});
   998     val sctxt = (Simplifier.context lthy' HOL_ss) addsimps (absrep :: @{thms fun_map.simps});
   956   val t = Goal.prove_internal [] ceq (fn _ => simp_tac sctxt 1)
   999     val t = Goal.prove_internal [] ceq (fn _ => simp_tac sctxt 1)
   957   val t_id = MetaSimplifier.rewrite_rule @{thms id_def_sym} t;
  1000     val t_id = MetaSimplifier.rewrite_rule @{thms id_def_sym} t;
   958  in
  1001   in
   959   singleton (ProofContext.export lthy' lthy) t_id
  1002     singleton (ProofContext.export lthy' lthy) t_id
   960  end
  1003   end
   961 *}
  1004 *}
   962 
  1005 
   963 ML {*
  1006 ML {*
   964 fun matches (ty1, ty2) =
  1007 fun matches (ty1, ty2) =
   965   Type.raw_instance (ty1, Logic.varifyT ty2);
  1008   Type.raw_instance (ty1, Logic.varifyT ty2);
  1030 end
  1073 end
  1031 *}
  1074 *}
  1032 
  1075 
  1033 
  1076 
  1034 ML {*
  1077 ML {*
  1035   fun lift_thm_note qty qty_name rsp_thms defs thm name lthy =
  1078 fun lift_thm_note qty qty_name rsp_thms defs thm name lthy =
  1036     let
  1079   let
  1037       val lifted_thm = lift_thm lthy qty qty_name rsp_thms defs thm;
  1080     val lifted_thm = lift_thm lthy qty qty_name rsp_thms defs thm;
  1038       val (_, lthy2) = note (name, lifted_thm) lthy;
  1081     val (_, lthy2) = note (name, lifted_thm) lthy;
  1039     in
  1082   in
  1040       lthy2
  1083     lthy2
  1041     end;
  1084   end
  1042 *}
  1085 *}
  1043 
  1086 
  1044 
  1087 
  1045 
  1088 
  1046 end
  1089 end