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