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