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