src/HOL/Quotient.thy
author haftmann
Tue Nov 09 14:02:13 2010 +0100 (2010-11-09)
changeset 40466 c6587375088e
parent 40031 2671cce4d25d
child 40602 91e583511113
permissions -rw-r--r--
type annotations in specifications; fun_rel_def is no simp rule by default; slightly changed fun_map_def; more on predicates on relation functions; proper HOL equations in definitions
     1 (*  Title:      Quotient.thy
     2     Author:     Cezary Kaliszyk and Christian Urban
     3 *)
     4 
     5 header {* Definition of Quotient Types *}
     6 
     7 theory Quotient
     8 imports Plain Hilbert_Choice Equiv_Relations
     9 uses
    10   ("Tools/Quotient/quotient_info.ML")
    11   ("Tools/Quotient/quotient_typ.ML")
    12   ("Tools/Quotient/quotient_def.ML")
    13   ("Tools/Quotient/quotient_term.ML")
    14   ("Tools/Quotient/quotient_tacs.ML")
    15 begin
    16 
    17 
    18 text {*
    19   Basic definition for equivalence relations
    20   that are represented by predicates.
    21 *}
    22 
    23 definition
    24   "reflp E \<longleftrightarrow> (\<forall>x. E x x)"
    25 
    26 lemma refl_reflp:
    27   "refl A \<longleftrightarrow> reflp (\<lambda>x y. (x, y) \<in> A)"
    28   by (simp add: refl_on_def reflp_def)
    29 
    30 definition
    31   "symp E \<longleftrightarrow> (\<forall>x y. E x y \<longrightarrow> E y x)"
    32 
    33 lemma sym_symp:
    34   "sym A \<longleftrightarrow> symp (\<lambda>x y. (x, y) \<in> A)"
    35   by (simp add: sym_def symp_def)
    36 
    37 definition
    38   "transp E \<longleftrightarrow> (\<forall>x y z. E x y \<and> E y z \<longrightarrow> E x z)"
    39 
    40 lemma trans_transp:
    41   "trans A \<longleftrightarrow> transp (\<lambda>x y. (x, y) \<in> A)"
    42   by (auto simp add: trans_def transp_def)
    43 
    44 definition
    45   "equivp E \<longleftrightarrow> (\<forall>x y. E x y = (E x = E y))"
    46 
    47 lemma equivp_reflp_symp_transp:
    48   shows "equivp E = (reflp E \<and> symp E \<and> transp E)"
    49   unfolding equivp_def reflp_def symp_def transp_def fun_eq_iff
    50   by blast
    51 
    52 lemma equiv_equivp:
    53   "equiv UNIV A \<longleftrightarrow> equivp (\<lambda>x y. (x, y) \<in> A)"
    54   by (simp add: equiv_def equivp_reflp_symp_transp refl_reflp sym_symp trans_transp)
    55 
    56 lemma equivp_reflp:
    57   shows "equivp E \<Longrightarrow> E x x"
    58   by (simp only: equivp_reflp_symp_transp reflp_def)
    59 
    60 lemma equivp_symp:
    61   shows "equivp E \<Longrightarrow> E x y \<Longrightarrow> E y x"
    62   by (simp add: equivp_def)
    63 
    64 lemma equivp_transp:
    65   shows "equivp E \<Longrightarrow> E x y \<Longrightarrow> E y z \<Longrightarrow> E x z"
    66   by (simp add: equivp_def)
    67 
    68 lemma equivpI:
    69   assumes "reflp R" "symp R" "transp R"
    70   shows "equivp R"
    71   using assms by (simp add: equivp_reflp_symp_transp)
    72 
    73 lemma identity_equivp:
    74   shows "equivp (op =)"
    75   unfolding equivp_def
    76   by auto
    77 
    78 text {* Partial equivalences *}
    79 
    80 definition
    81   "part_equivp E \<longleftrightarrow> (\<exists>x. E x x) \<and> (\<forall>x y. E x y = (E x x \<and> E y y \<and> (E x = E y)))"
    82 
    83 lemma equivp_implies_part_equivp:
    84   assumes a: "equivp E"
    85   shows "part_equivp E"
    86   using a
    87   unfolding equivp_def part_equivp_def
    88   by auto
    89 
    90 lemma part_equivp_symp:
    91   assumes e: "part_equivp R"
    92   and a: "R x y"
    93   shows "R y x"
    94   using e[simplified part_equivp_def] a
    95   by (metis)
    96 
    97 lemma part_equivp_typedef:
    98   shows "part_equivp R \<Longrightarrow> \<exists>d. d \<in> (\<lambda>c. \<exists>x. R x x \<and> c = R x)"
    99   unfolding part_equivp_def mem_def
   100   apply clarify
   101   apply (intro exI)
   102   apply (rule conjI)
   103   apply assumption
   104   apply (rule refl)
   105   done
   106 
   107 lemma part_equivp_refl_symp_transp:
   108   shows "part_equivp E \<longleftrightarrow> ((\<exists>x. E x x) \<and> symp E \<and> transp E)"
   109 proof
   110   assume "part_equivp E"
   111   then show "(\<exists>x. E x x) \<and> symp E \<and> transp E"
   112   unfolding part_equivp_def symp_def transp_def
   113   by metis
   114 next
   115   assume a: "(\<exists>x. E x x) \<and> symp E \<and> transp E"
   116   then have b: "(\<forall>x y. E x y \<longrightarrow> E y x)" and c: "(\<forall>x y z. E x y \<and> E y z \<longrightarrow> E x z)"
   117     unfolding symp_def transp_def by (metis, metis)
   118   have "(\<forall>x y. E x y = (E x x \<and> E y y \<and> E x = E y))"
   119   proof (intro allI iffI conjI)
   120     fix x y
   121     assume d: "E x y"
   122     then show "E x x" using b c by metis
   123     show "E y y" using b c d by metis
   124     show "E x = E y" unfolding fun_eq_iff using b c d by metis
   125   next
   126     fix x y
   127     assume "E x x \<and> E y y \<and> E x = E y"
   128     then show "E x y" using b c by metis
   129   qed
   130   then show "part_equivp E" unfolding part_equivp_def using a by metis
   131 qed
   132 
   133 lemma part_equivpI:
   134   assumes "\<exists>x. R x x" "symp R" "transp R"
   135   shows "part_equivp R"
   136   using assms by (simp add: part_equivp_refl_symp_transp)
   137 
   138 text {* Composition of Relations *}
   139 
   140 abbreviation
   141   rel_conj (infixr "OOO" 75)
   142 where
   143   "r1 OOO r2 \<equiv> r1 OO r2 OO r1"
   144 
   145 lemma eq_comp_r:
   146   shows "((op =) OOO R) = R"
   147   by (auto simp add: fun_eq_iff)
   148 
   149 subsection {* Respects predicate *}
   150 
   151 definition
   152   Respects :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a set"
   153 where
   154   "Respects R x = R x x"
   155 
   156 lemma in_respects:
   157   shows "x \<in> Respects R \<longleftrightarrow> R x x"
   158   unfolding mem_def Respects_def
   159   by simp
   160 
   161 subsection {* Function map and function relation *}
   162 
   163 definition
   164   fun_map :: "('c \<Rightarrow> 'a) \<Rightarrow> ('b \<Rightarrow> 'd) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'c \<Rightarrow> 'd" (infixr "--->" 55)
   165 where
   166   "fun_map f g = (\<lambda>h. g \<circ> h \<circ> f)"
   167 
   168 lemma fun_map_apply [simp]:
   169   "(f ---> g) h x = g (h (f x))"
   170   by (simp add: fun_map_def)
   171 
   172 lemma fun_map_id:
   173   "(id ---> id) = id"
   174   by (simp add: fun_eq_iff id_def)
   175 
   176 definition
   177   fun_rel :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('b \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> bool" (infixr "===>" 55)
   178 where
   179   "fun_rel E1 E2 = (\<lambda>f g. \<forall>x y. E1 x y \<longrightarrow> E2 (f x) (g y))"
   180 
   181 lemma fun_relI [intro]:
   182   assumes "\<And>x y. E1 x y \<Longrightarrow> E2 (f x) (g y)"
   183   shows "(E1 ===> E2) f g"
   184   using assms by (simp add: fun_rel_def)
   185 
   186 lemma fun_relE:
   187   assumes "(E1 ===> E2) f g" and "E1 x y"
   188   obtains "E2 (f x) (g y)"
   189   using assms by (simp add: fun_rel_def)
   190 
   191 lemma fun_rel_eq:
   192   shows "((op =) ===> (op =)) = (op =)"
   193   by (auto simp add: fun_eq_iff elim: fun_relE)
   194 
   195 
   196 subsection {* Quotient Predicate *}
   197 
   198 definition
   199   "Quotient E Abs Rep \<longleftrightarrow>
   200      (\<forall>a. Abs (Rep a) = a) \<and> (\<forall>a. E (Rep a) (Rep a)) \<and>
   201      (\<forall>r s. E r s = (E r r \<and> E s s \<and> (Abs r = Abs s)))"
   202 
   203 lemma Quotient_abs_rep:
   204   assumes a: "Quotient E Abs Rep"
   205   shows "Abs (Rep a) = a"
   206   using a
   207   unfolding Quotient_def
   208   by simp
   209 
   210 lemma Quotient_rep_reflp:
   211   assumes a: "Quotient E Abs Rep"
   212   shows "E (Rep a) (Rep a)"
   213   using a
   214   unfolding Quotient_def
   215   by blast
   216 
   217 lemma Quotient_rel:
   218   assumes a: "Quotient E Abs Rep"
   219   shows " E r s = (E r r \<and> E s s \<and> (Abs r = Abs s))"
   220   using a
   221   unfolding Quotient_def
   222   by blast
   223 
   224 lemma Quotient_rel_rep:
   225   assumes a: "Quotient R Abs Rep"
   226   shows "R (Rep a) (Rep b) = (a = b)"
   227   using a
   228   unfolding Quotient_def
   229   by metis
   230 
   231 lemma Quotient_rep_abs:
   232   assumes a: "Quotient R Abs Rep"
   233   shows "R r r \<Longrightarrow> R (Rep (Abs r)) r"
   234   using a unfolding Quotient_def
   235   by blast
   236 
   237 lemma Quotient_rel_abs:
   238   assumes a: "Quotient E Abs Rep"
   239   shows "E r s \<Longrightarrow> Abs r = Abs s"
   240   using a unfolding Quotient_def
   241   by blast
   242 
   243 lemma Quotient_symp:
   244   assumes a: "Quotient E Abs Rep"
   245   shows "symp E"
   246   using a unfolding Quotient_def symp_def
   247   by metis
   248 
   249 lemma Quotient_transp:
   250   assumes a: "Quotient E Abs Rep"
   251   shows "transp E"
   252   using a unfolding Quotient_def transp_def
   253   by metis
   254 
   255 lemma identity_quotient:
   256   shows "Quotient (op =) id id"
   257   unfolding Quotient_def id_def
   258   by blast
   259 
   260 lemma fun_quotient:
   261   assumes q1: "Quotient R1 abs1 rep1"
   262   and     q2: "Quotient R2 abs2 rep2"
   263   shows "Quotient (R1 ===> R2) (rep1 ---> abs2) (abs1 ---> rep2)"
   264 proof -
   265   have "\<And>a. (rep1 ---> abs2) ((abs1 ---> rep2) a) = a"
   266     using q1 q2 by (simp add: Quotient_def fun_eq_iff)
   267   moreover
   268   have "\<And>a. (R1 ===> R2) ((abs1 ---> rep2) a) ((abs1 ---> rep2) a)"
   269     by (rule fun_relI)
   270       (insert q1 q2 Quotient_rel_abs [of R1 abs1 rep1] Quotient_rel_rep [of R2 abs2 rep2],
   271         simp (no_asm) add: Quotient_def, simp)
   272   moreover
   273   have "\<And>r s. (R1 ===> R2) r s = ((R1 ===> R2) r r \<and> (R1 ===> R2) s s \<and>
   274         (rep1 ---> abs2) r  = (rep1 ---> abs2) s)"
   275     apply(auto simp add: fun_rel_def fun_eq_iff)
   276     using q1 q2 unfolding Quotient_def
   277     apply(metis)
   278     using q1 q2 unfolding Quotient_def
   279     apply(metis)
   280     using q1 q2 unfolding Quotient_def
   281     apply(metis)
   282     using q1 q2 unfolding Quotient_def
   283     apply(metis)
   284     done
   285   ultimately
   286   show "Quotient (R1 ===> R2) (rep1 ---> abs2) (abs1 ---> rep2)"
   287     unfolding Quotient_def by blast
   288 qed
   289 
   290 lemma abs_o_rep:
   291   assumes a: "Quotient R Abs Rep"
   292   shows "Abs o Rep = id"
   293   unfolding fun_eq_iff
   294   by (simp add: Quotient_abs_rep[OF a])
   295 
   296 lemma equals_rsp:
   297   assumes q: "Quotient R Abs Rep"
   298   and     a: "R xa xb" "R ya yb"
   299   shows "R xa ya = R xb yb"
   300   using a Quotient_symp[OF q] Quotient_transp[OF q]
   301   unfolding symp_def transp_def
   302   by blast
   303 
   304 lemma lambda_prs:
   305   assumes q1: "Quotient R1 Abs1 Rep1"
   306   and     q2: "Quotient R2 Abs2 Rep2"
   307   shows "(Rep1 ---> Abs2) (\<lambda>x. Rep2 (f (Abs1 x))) = (\<lambda>x. f x)"
   308   unfolding fun_eq_iff
   309   using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2]
   310   by (simp add:)
   311 
   312 lemma lambda_prs1:
   313   assumes q1: "Quotient R1 Abs1 Rep1"
   314   and     q2: "Quotient R2 Abs2 Rep2"
   315   shows "(Rep1 ---> Abs2) (\<lambda>x. (Abs1 ---> Rep2) f x) = (\<lambda>x. f x)"
   316   unfolding fun_eq_iff
   317   using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2]
   318   by (simp add:)
   319 
   320 lemma rep_abs_rsp:
   321   assumes q: "Quotient R Abs Rep"
   322   and     a: "R x1 x2"
   323   shows "R x1 (Rep (Abs x2))"
   324   using a Quotient_rel[OF q] Quotient_abs_rep[OF q] Quotient_rep_reflp[OF q]
   325   by metis
   326 
   327 lemma rep_abs_rsp_left:
   328   assumes q: "Quotient R Abs Rep"
   329   and     a: "R x1 x2"
   330   shows "R (Rep (Abs x1)) x2"
   331   using a Quotient_rel[OF q] Quotient_abs_rep[OF q] Quotient_rep_reflp[OF q]
   332   by metis
   333 
   334 text{*
   335   In the following theorem R1 can be instantiated with anything,
   336   but we know some of the types of the Rep and Abs functions;
   337   so by solving Quotient assumptions we can get a unique R1 that
   338   will be provable; which is why we need to use @{text apply_rsp} and
   339   not the primed version *}
   340 
   341 lemma apply_rsp:
   342   fixes f g::"'a \<Rightarrow> 'c"
   343   assumes q: "Quotient R1 Abs1 Rep1"
   344   and     a: "(R1 ===> R2) f g" "R1 x y"
   345   shows "R2 (f x) (g y)"
   346   using a by (auto elim: fun_relE)
   347 
   348 lemma apply_rsp':
   349   assumes a: "(R1 ===> R2) f g" "R1 x y"
   350   shows "R2 (f x) (g y)"
   351   using a by (auto elim: fun_relE)
   352 
   353 subsection {* lemmas for regularisation of ball and bex *}
   354 
   355 lemma ball_reg_eqv:
   356   fixes P :: "'a \<Rightarrow> bool"
   357   assumes a: "equivp R"
   358   shows "Ball (Respects R) P = (All P)"
   359   using a
   360   unfolding equivp_def
   361   by (auto simp add: in_respects)
   362 
   363 lemma bex_reg_eqv:
   364   fixes P :: "'a \<Rightarrow> bool"
   365   assumes a: "equivp R"
   366   shows "Bex (Respects R) P = (Ex P)"
   367   using a
   368   unfolding equivp_def
   369   by (auto simp add: in_respects)
   370 
   371 lemma ball_reg_right:
   372   assumes a: "\<And>x. R x \<Longrightarrow> P x \<longrightarrow> Q x"
   373   shows "All P \<longrightarrow> Ball R Q"
   374   using a by (metis Collect_def Collect_mem_eq)
   375 
   376 lemma bex_reg_left:
   377   assumes a: "\<And>x. R x \<Longrightarrow> Q x \<longrightarrow> P x"
   378   shows "Bex R Q \<longrightarrow> Ex P"
   379   using a by (metis Collect_def Collect_mem_eq)
   380 
   381 lemma ball_reg_left:
   382   assumes a: "equivp R"
   383   shows "(\<And>x. (Q x \<longrightarrow> P x)) \<Longrightarrow> Ball (Respects R) Q \<longrightarrow> All P"
   384   using a by (metis equivp_reflp in_respects)
   385 
   386 lemma bex_reg_right:
   387   assumes a: "equivp R"
   388   shows "(\<And>x. (Q x \<longrightarrow> P x)) \<Longrightarrow> Ex Q \<longrightarrow> Bex (Respects R) P"
   389   using a by (metis equivp_reflp in_respects)
   390 
   391 lemma ball_reg_eqv_range:
   392   fixes P::"'a \<Rightarrow> bool"
   393   and x::"'a"
   394   assumes a: "equivp R2"
   395   shows   "(Ball (Respects (R1 ===> R2)) (\<lambda>f. P (f x)) = All (\<lambda>f. P (f x)))"
   396   apply(rule iffI)
   397   apply(rule allI)
   398   apply(drule_tac x="\<lambda>y. f x" in bspec)
   399   apply(simp add: in_respects fun_rel_def)
   400   apply(rule impI)
   401   using a equivp_reflp_symp_transp[of "R2"]
   402   apply(simp add: reflp_def)
   403   apply(simp)
   404   apply(simp)
   405   done
   406 
   407 lemma bex_reg_eqv_range:
   408   assumes a: "equivp R2"
   409   shows   "(Bex (Respects (R1 ===> R2)) (\<lambda>f. P (f x)) = Ex (\<lambda>f. P (f x)))"
   410   apply(auto)
   411   apply(rule_tac x="\<lambda>y. f x" in bexI)
   412   apply(simp)
   413   apply(simp add: Respects_def in_respects fun_rel_def)
   414   apply(rule impI)
   415   using a equivp_reflp_symp_transp[of "R2"]
   416   apply(simp add: reflp_def)
   417   done
   418 
   419 (* Next four lemmas are unused *)
   420 lemma all_reg:
   421   assumes a: "!x :: 'a. (P x --> Q x)"
   422   and     b: "All P"
   423   shows "All Q"
   424   using a b by (metis)
   425 
   426 lemma ex_reg:
   427   assumes a: "!x :: 'a. (P x --> Q x)"
   428   and     b: "Ex P"
   429   shows "Ex Q"
   430   using a b by metis
   431 
   432 lemma ball_reg:
   433   assumes a: "!x :: 'a. (R x --> P x --> Q x)"
   434   and     b: "Ball R P"
   435   shows "Ball R Q"
   436   using a b by (metis Collect_def Collect_mem_eq)
   437 
   438 lemma bex_reg:
   439   assumes a: "!x :: 'a. (R x --> P x --> Q x)"
   440   and     b: "Bex R P"
   441   shows "Bex R Q"
   442   using a b by (metis Collect_def Collect_mem_eq)
   443 
   444 
   445 lemma ball_all_comm:
   446   assumes "\<And>y. (\<forall>x\<in>P. A x y) \<longrightarrow> (\<forall>x. B x y)"
   447   shows "(\<forall>x\<in>P. \<forall>y. A x y) \<longrightarrow> (\<forall>x. \<forall>y. B x y)"
   448   using assms by auto
   449 
   450 lemma bex_ex_comm:
   451   assumes "(\<exists>y. \<exists>x. A x y) \<longrightarrow> (\<exists>y. \<exists>x\<in>P. B x y)"
   452   shows "(\<exists>x. \<exists>y. A x y) \<longrightarrow> (\<exists>x\<in>P. \<exists>y. B x y)"
   453   using assms by auto
   454 
   455 subsection {* Bounded abstraction *}
   456 
   457 definition
   458   Babs :: "'a set \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b"
   459 where
   460   "x \<in> p \<Longrightarrow> Babs p m x = m x"
   461 
   462 lemma babs_rsp:
   463   assumes q: "Quotient R1 Abs1 Rep1"
   464   and     a: "(R1 ===> R2) f g"
   465   shows      "(R1 ===> R2) (Babs (Respects R1) f) (Babs (Respects R1) g)"
   466   apply (auto simp add: Babs_def in_respects fun_rel_def)
   467   apply (subgoal_tac "x \<in> Respects R1 \<and> y \<in> Respects R1")
   468   using a apply (simp add: Babs_def fun_rel_def)
   469   apply (simp add: in_respects fun_rel_def)
   470   using Quotient_rel[OF q]
   471   by metis
   472 
   473 lemma babs_prs:
   474   assumes q1: "Quotient R1 Abs1 Rep1"
   475   and     q2: "Quotient R2 Abs2 Rep2"
   476   shows "((Rep1 ---> Abs2) (Babs (Respects R1) ((Abs1 ---> Rep2) f))) = f"
   477   apply (rule ext)
   478   apply (simp add:)
   479   apply (subgoal_tac "Rep1 x \<in> Respects R1")
   480   apply (simp add: Babs_def Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2])
   481   apply (simp add: in_respects Quotient_rel_rep[OF q1])
   482   done
   483 
   484 lemma babs_simp:
   485   assumes q: "Quotient R1 Abs Rep"
   486   shows "((R1 ===> R2) (Babs (Respects R1) f) (Babs (Respects R1) g)) = ((R1 ===> R2) f g)"
   487   apply(rule iffI)
   488   apply(simp_all only: babs_rsp[OF q])
   489   apply(auto simp add: Babs_def fun_rel_def)
   490   apply (subgoal_tac "x \<in> Respects R1 \<and> y \<in> Respects R1")
   491   apply(metis Babs_def)
   492   apply (simp add: in_respects)
   493   using Quotient_rel[OF q]
   494   by metis
   495 
   496 (* If a user proves that a particular functional relation
   497    is an equivalence this may be useful in regularising *)
   498 lemma babs_reg_eqv:
   499   shows "equivp R \<Longrightarrow> Babs (Respects R) P = P"
   500   by (simp add: fun_eq_iff Babs_def in_respects equivp_reflp)
   501 
   502 
   503 (* 3 lemmas needed for proving repabs_inj *)
   504 lemma ball_rsp:
   505   assumes a: "(R ===> (op =)) f g"
   506   shows "Ball (Respects R) f = Ball (Respects R) g"
   507   using a by (auto simp add: Ball_def in_respects elim: fun_relE)
   508 
   509 lemma bex_rsp:
   510   assumes a: "(R ===> (op =)) f g"
   511   shows "(Bex (Respects R) f = Bex (Respects R) g)"
   512   using a by (auto simp add: Bex_def in_respects elim: fun_relE)
   513 
   514 lemma bex1_rsp:
   515   assumes a: "(R ===> (op =)) f g"
   516   shows "Ex1 (\<lambda>x. x \<in> Respects R \<and> f x) = Ex1 (\<lambda>x. x \<in> Respects R \<and> g x)"
   517   using a by (auto elim: fun_relE simp add: Ex1_def in_respects) 
   518 
   519 (* 2 lemmas needed for cleaning of quantifiers *)
   520 lemma all_prs:
   521   assumes a: "Quotient R absf repf"
   522   shows "Ball (Respects R) ((absf ---> id) f) = All f"
   523   using a unfolding Quotient_def Ball_def in_respects id_apply comp_def fun_map_def
   524   by metis
   525 
   526 lemma ex_prs:
   527   assumes a: "Quotient R absf repf"
   528   shows "Bex (Respects R) ((absf ---> id) f) = Ex f"
   529   using a unfolding Quotient_def Bex_def in_respects id_apply comp_def fun_map_def
   530   by metis
   531 
   532 subsection {* @{text Bex1_rel} quantifier *}
   533 
   534 definition
   535   Bex1_rel :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
   536 where
   537   "Bex1_rel R P \<longleftrightarrow> (\<exists>x \<in> Respects R. P x) \<and> (\<forall>x \<in> Respects R. \<forall>y \<in> Respects R. ((P x \<and> P y) \<longrightarrow> (R x y)))"
   538 
   539 lemma bex1_rel_aux:
   540   "\<lbrakk>\<forall>xa ya. R xa ya \<longrightarrow> x xa = y ya; Bex1_rel R x\<rbrakk> \<Longrightarrow> Bex1_rel R y"
   541   unfolding Bex1_rel_def
   542   apply (erule conjE)+
   543   apply (erule bexE)
   544   apply rule
   545   apply (rule_tac x="xa" in bexI)
   546   apply metis
   547   apply metis
   548   apply rule+
   549   apply (erule_tac x="xaa" in ballE)
   550   prefer 2
   551   apply (metis)
   552   apply (erule_tac x="ya" in ballE)
   553   prefer 2
   554   apply (metis)
   555   apply (metis in_respects)
   556   done
   557 
   558 lemma bex1_rel_aux2:
   559   "\<lbrakk>\<forall>xa ya. R xa ya \<longrightarrow> x xa = y ya; Bex1_rel R y\<rbrakk> \<Longrightarrow> Bex1_rel R x"
   560   unfolding Bex1_rel_def
   561   apply (erule conjE)+
   562   apply (erule bexE)
   563   apply rule
   564   apply (rule_tac x="xa" in bexI)
   565   apply metis
   566   apply metis
   567   apply rule+
   568   apply (erule_tac x="xaa" in ballE)
   569   prefer 2
   570   apply (metis)
   571   apply (erule_tac x="ya" in ballE)
   572   prefer 2
   573   apply (metis)
   574   apply (metis in_respects)
   575   done
   576 
   577 lemma bex1_rel_rsp:
   578   assumes a: "Quotient R absf repf"
   579   shows "((R ===> op =) ===> op =) (Bex1_rel R) (Bex1_rel R)"
   580   apply (simp add: fun_rel_def)
   581   apply clarify
   582   apply rule
   583   apply (simp_all add: bex1_rel_aux bex1_rel_aux2)
   584   apply (erule bex1_rel_aux2)
   585   apply assumption
   586   done
   587 
   588 
   589 lemma ex1_prs:
   590   assumes a: "Quotient R absf repf"
   591   shows "((absf ---> id) ---> id) (Bex1_rel R) f = Ex1 f"
   592 apply (simp add:)
   593 apply (subst Bex1_rel_def)
   594 apply (subst Bex_def)
   595 apply (subst Ex1_def)
   596 apply simp
   597 apply rule
   598  apply (erule conjE)+
   599  apply (erule_tac exE)
   600  apply (erule conjE)
   601  apply (subgoal_tac "\<forall>y. R y y \<longrightarrow> f (absf y) \<longrightarrow> R x y")
   602   apply (rule_tac x="absf x" in exI)
   603   apply (simp)
   604   apply rule+
   605   using a unfolding Quotient_def
   606   apply metis
   607  apply rule+
   608  apply (erule_tac x="x" in ballE)
   609   apply (erule_tac x="y" in ballE)
   610    apply simp
   611   apply (simp add: in_respects)
   612  apply (simp add: in_respects)
   613 apply (erule_tac exE)
   614  apply rule
   615  apply (rule_tac x="repf x" in exI)
   616  apply (simp only: in_respects)
   617   apply rule
   618  apply (metis Quotient_rel_rep[OF a])
   619 using a unfolding Quotient_def apply (simp)
   620 apply rule+
   621 using a unfolding Quotient_def in_respects
   622 apply metis
   623 done
   624 
   625 lemma bex1_bexeq_reg:
   626   shows "(\<exists>!x\<in>Respects R. P x) \<longrightarrow> (Bex1_rel R (\<lambda>x. P x))"
   627   apply (simp add: Ex1_def Bex1_rel_def in_respects)
   628   apply clarify
   629   apply auto
   630   apply (rule bexI)
   631   apply assumption
   632   apply (simp add: in_respects)
   633   apply (simp add: in_respects)
   634   apply auto
   635   done
   636 
   637 lemma bex1_bexeq_reg_eqv:
   638   assumes a: "equivp R"
   639   shows "(\<exists>!x. P x) \<longrightarrow> Bex1_rel R P"
   640   using equivp_reflp[OF a]
   641   apply (intro impI)
   642   apply (elim ex1E)
   643   apply (rule mp[OF bex1_bexeq_reg])
   644   apply (rule_tac a="x" in ex1I)
   645   apply (subst in_respects)
   646   apply (rule conjI)
   647   apply assumption
   648   apply assumption
   649   apply clarify
   650   apply (erule_tac x="xa" in allE)
   651   apply simp
   652   done
   653 
   654 subsection {* Various respects and preserve lemmas *}
   655 
   656 lemma quot_rel_rsp:
   657   assumes a: "Quotient R Abs Rep"
   658   shows "(R ===> R ===> op =) R R"
   659   apply(rule fun_relI)+
   660   apply(rule equals_rsp[OF a])
   661   apply(assumption)+
   662   done
   663 
   664 lemma o_prs:
   665   assumes q1: "Quotient R1 Abs1 Rep1"
   666   and     q2: "Quotient R2 Abs2 Rep2"
   667   and     q3: "Quotient R3 Abs3 Rep3"
   668   shows "((Abs2 ---> Rep3) ---> (Abs1 ---> Rep2) ---> (Rep1 ---> Abs3)) op \<circ> = op \<circ>"
   669   and   "(id ---> (Abs1 ---> id) ---> Rep1 ---> id) op \<circ> = op \<circ>"
   670   using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2] Quotient_abs_rep[OF q3]
   671   by (simp_all add: fun_eq_iff)
   672 
   673 lemma o_rsp:
   674   "((R2 ===> R3) ===> (R1 ===> R2) ===> (R1 ===> R3)) op \<circ> op \<circ>"
   675   "(op = ===> (R1 ===> op =) ===> R1 ===> op =) op \<circ> op \<circ>"
   676   by (auto intro!: fun_relI elim: fun_relE)
   677 
   678 lemma cond_prs:
   679   assumes a: "Quotient R absf repf"
   680   shows "absf (if a then repf b else repf c) = (if a then b else c)"
   681   using a unfolding Quotient_def by auto
   682 
   683 lemma if_prs:
   684   assumes q: "Quotient R Abs Rep"
   685   shows "(id ---> Rep ---> Rep ---> Abs) If = If"
   686   using Quotient_abs_rep[OF q]
   687   by (auto simp add: fun_eq_iff)
   688 
   689 lemma if_rsp:
   690   assumes q: "Quotient R Abs Rep"
   691   shows "(op = ===> R ===> R ===> R) If If"
   692   by (auto intro!: fun_relI)
   693 
   694 lemma let_prs:
   695   assumes q1: "Quotient R1 Abs1 Rep1"
   696   and     q2: "Quotient R2 Abs2 Rep2"
   697   shows "(Rep2 ---> (Abs2 ---> Rep1) ---> Abs1) Let = Let"
   698   using Quotient_abs_rep[OF q1] Quotient_abs_rep[OF q2]
   699   by (auto simp add: fun_eq_iff)
   700 
   701 lemma let_rsp:
   702   shows "(R1 ===> (R1 ===> R2) ===> R2) Let Let"
   703   by (auto intro!: fun_relI elim: fun_relE)
   704 
   705 lemma mem_rsp:
   706   shows "(R1 ===> (R1 ===> R2) ===> R2) op \<in> op \<in>"
   707   by (auto intro!: fun_relI elim: fun_relE simp add: mem_def)
   708 
   709 lemma mem_prs:
   710   assumes a1: "Quotient R1 Abs1 Rep1"
   711   and     a2: "Quotient R2 Abs2 Rep2"
   712   shows "(Rep1 ---> (Abs1 ---> Rep2) ---> Abs2) op \<in> = op \<in>"
   713   by (simp add: fun_eq_iff mem_def Quotient_abs_rep[OF a1] Quotient_abs_rep[OF a2])
   714 
   715 lemma id_rsp:
   716   shows "(R ===> R) id id"
   717   by (auto intro: fun_relI)
   718 
   719 lemma id_prs:
   720   assumes a: "Quotient R Abs Rep"
   721   shows "(Rep ---> Abs) id = id"
   722   by (simp add: fun_eq_iff Quotient_abs_rep [OF a])
   723 
   724 
   725 locale quot_type =
   726   fixes R :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
   727   and   Abs :: "('a \<Rightarrow> bool) \<Rightarrow> 'b"
   728   and   Rep :: "'b \<Rightarrow> ('a \<Rightarrow> bool)"
   729   assumes equivp: "part_equivp R"
   730   and     rep_prop: "\<And>y. \<exists>x. R x x \<and> Rep y = R x"
   731   and     rep_inverse: "\<And>x. Abs (Rep x) = x"
   732   and     abs_inverse: "\<And>c. (\<exists>x. ((R x x) \<and> (c = R x))) \<Longrightarrow> (Rep (Abs c)) = c"
   733   and     rep_inject: "\<And>x y. (Rep x = Rep y) = (x = y)"
   734 begin
   735 
   736 definition
   737   abs :: "'a \<Rightarrow> 'b"
   738 where
   739   "abs x = Abs (R x)"
   740 
   741 definition
   742   rep :: "'b \<Rightarrow> 'a"
   743 where
   744   "rep a = Eps (Rep a)"
   745 
   746 lemma homeier5:
   747   assumes a: "R r r"
   748   shows "Rep (Abs (R r)) = R r"
   749   apply (subst abs_inverse)
   750   using a by auto
   751 
   752 theorem homeier6:
   753   assumes a: "R r r"
   754   and b: "R s s"
   755   shows "Abs (R r) = Abs (R s) \<longleftrightarrow> R r = R s"
   756   by (metis a b homeier5)
   757 
   758 theorem homeier8:
   759   assumes "R r r"
   760   shows "R (Eps (R r)) = R r"
   761   using assms equivp[simplified part_equivp_def]
   762   apply clarify
   763   by (metis assms exE_some)
   764 
   765 lemma Quotient:
   766   shows "Quotient R abs rep"
   767   unfolding Quotient_def abs_def rep_def
   768   proof (intro conjI allI)
   769     fix a r s
   770     show "Abs (R (Eps (Rep a))) = a"
   771       by (metis equivp exE_some part_equivp_def rep_inverse rep_prop)
   772     show "R r s \<longleftrightarrow> R r r \<and> R s s \<and> (Abs (R r) = Abs (R s))"
   773       by (metis homeier6 equivp[simplified part_equivp_def])
   774     show "R (Eps (Rep a)) (Eps (Rep a))" proof -
   775       obtain x where r: "R x x" and rep: "Rep a = R x" using rep_prop[of a] by auto
   776       have "R (Eps (R x)) x" using homeier8 r by simp
   777       then have "R x (Eps (R x))" using part_equivp_symp[OF equivp] by fast
   778       then have "R (Eps (R x)) (Eps (R x))" using homeier8[OF r] by simp
   779       then show "R (Eps (Rep a)) (Eps (Rep a))" using rep by simp
   780     qed
   781   qed
   782 
   783 end
   784 
   785 
   786 subsection {* ML setup *}
   787 
   788 text {* Auxiliary data for the quotient package *}
   789 
   790 use "Tools/Quotient/quotient_info.ML"
   791 
   792 declare [[map "fun" = (fun_map, fun_rel)]]
   793 
   794 lemmas [quot_thm] = fun_quotient
   795 lemmas [quot_respect] = quot_rel_rsp if_rsp o_rsp let_rsp mem_rsp id_rsp
   796 lemmas [quot_preserve] = if_prs o_prs let_prs mem_prs id_prs
   797 lemmas [quot_equiv] = identity_equivp
   798 
   799 
   800 text {* Lemmas about simplifying id's. *}
   801 lemmas [id_simps] =
   802   id_def[symmetric]
   803   fun_map_id
   804   id_apply
   805   id_o
   806   o_id
   807   eq_comp_r
   808 
   809 text {* Translation functions for the lifting process. *}
   810 use "Tools/Quotient/quotient_term.ML"
   811 
   812 
   813 text {* Definitions of the quotient types. *}
   814 use "Tools/Quotient/quotient_typ.ML"
   815 
   816 
   817 text {* Definitions for quotient constants. *}
   818 use "Tools/Quotient/quotient_def.ML"
   819 
   820 
   821 text {*
   822   An auxiliary constant for recording some information
   823   about the lifted theorem in a tactic.
   824 *}
   825 definition
   826   Quot_True :: "'a \<Rightarrow> bool"
   827 where
   828   "Quot_True x \<longleftrightarrow> True"
   829 
   830 lemma
   831   shows QT_all: "Quot_True (All P) \<Longrightarrow> Quot_True P"
   832   and   QT_ex:  "Quot_True (Ex P) \<Longrightarrow> Quot_True P"
   833   and   QT_ex1: "Quot_True (Ex1 P) \<Longrightarrow> Quot_True P"
   834   and   QT_lam: "Quot_True (\<lambda>x. P x) \<Longrightarrow> (\<And>x. Quot_True (P x))"
   835   and   QT_ext: "(\<And>x. Quot_True (a x) \<Longrightarrow> f x = g x) \<Longrightarrow> (Quot_True a \<Longrightarrow> f = g)"
   836   by (simp_all add: Quot_True_def ext)
   837 
   838 lemma QT_imp: "Quot_True a \<equiv> Quot_True b"
   839   by (simp add: Quot_True_def)
   840 
   841 
   842 text {* Tactics for proving the lifted theorems *}
   843 use "Tools/Quotient/quotient_tacs.ML"
   844 
   845 subsection {* Methods / Interface *}
   846 
   847 method_setup lifting =
   848   {* Attrib.thms >> (fn thms => fn ctxt => 
   849        SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.lift_tac ctxt [] thms))) *}
   850   {* lifts theorems to quotient types *}
   851 
   852 method_setup lifting_setup =
   853   {* Attrib.thm >> (fn thm => fn ctxt => 
   854        SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.lift_procedure_tac ctxt [] thm))) *}
   855   {* sets up the three goals for the quotient lifting procedure *}
   856 
   857 method_setup descending =
   858   {* Scan.succeed (fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.descend_tac ctxt []))) *}
   859   {* decends theorems to the raw level *}
   860 
   861 method_setup descending_setup =
   862   {* Scan.succeed (fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.descend_procedure_tac ctxt []))) *}
   863   {* sets up the three goals for the decending theorems *}
   864 
   865 method_setup regularize =
   866   {* Scan.succeed (fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.regularize_tac ctxt))) *}
   867   {* proves the regularization goals from the quotient lifting procedure *}
   868 
   869 method_setup injection =
   870   {* Scan.succeed (fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.all_injection_tac ctxt))) *}
   871   {* proves the rep/abs injection goals from the quotient lifting procedure *}
   872 
   873 method_setup cleaning =
   874   {* Scan.succeed (fn ctxt => SIMPLE_METHOD (HEADGOAL (Quotient_Tacs.clean_tac ctxt))) *}
   875   {* proves the cleaning goals from the quotient lifting procedure *}
   876 
   877 attribute_setup quot_lifted =
   878   {* Scan.succeed Quotient_Tacs.lifted_attrib *}
   879   {* lifts theorems to quotient types *}
   880 
   881 no_notation
   882   rel_conj (infixr "OOO" 75) and
   883   fun_map (infixr "--->" 55) and
   884   fun_rel (infixr "===>" 55)
   885 
   886 end