src/HOL/Lifting.thy
author kuncar
Wed May 15 12:10:39 2013 +0200 (2013-05-15)
changeset 51994 82cc2aeb7d13
parent 51956 a4d81cdebf8b
child 52036 1aa2e40df9ff
permissions -rw-r--r--
stronger reflexivity prover
kuncar@47308
     1
(*  Title:      HOL/Lifting.thy
kuncar@47308
     2
    Author:     Brian Huffman and Ondrej Kuncar
kuncar@47308
     3
    Author:     Cezary Kaliszyk and Christian Urban
kuncar@47308
     4
*)
kuncar@47308
     5
kuncar@47308
     6
header {* Lifting package *}
kuncar@47308
     7
kuncar@47308
     8
theory Lifting
haftmann@51112
     9
imports Equiv_Relations Transfer
kuncar@47308
    10
keywords
kuncar@51374
    11
  "parametric" and
kuncar@47308
    12
  "print_quotmaps" "print_quotients" :: diag and
kuncar@47308
    13
  "lift_definition" :: thy_goal and
kuncar@47308
    14
  "setup_lifting" :: thy_decl
kuncar@47308
    15
begin
kuncar@47308
    16
huffman@47325
    17
subsection {* Function map *}
kuncar@47308
    18
kuncar@47308
    19
notation map_fun (infixr "--->" 55)
kuncar@47308
    20
kuncar@47308
    21
lemma map_fun_id:
kuncar@47308
    22
  "(id ---> id) = id"
kuncar@47308
    23
  by (simp add: fun_eq_iff)
kuncar@47308
    24
kuncar@51994
    25
subsection {* Other predicates on relations *}
kuncar@51994
    26
kuncar@51994
    27
definition left_total :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
kuncar@51994
    28
  where "left_total R \<longleftrightarrow> (\<forall>x. \<exists>y. R x y)"
kuncar@51994
    29
kuncar@51994
    30
lemma left_totalI:
kuncar@51994
    31
  "(\<And>x. \<exists>y. R x y) \<Longrightarrow> left_total R"
kuncar@51994
    32
unfolding left_total_def by blast
kuncar@51994
    33
kuncar@51994
    34
lemma left_totalE:
kuncar@51994
    35
  assumes "left_total R"
kuncar@51994
    36
  obtains "(\<And>x. \<exists>y. R x y)"
kuncar@51994
    37
using assms unfolding left_total_def by blast
kuncar@51994
    38
kuncar@51994
    39
definition left_unique :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
kuncar@51994
    40
  where "left_unique R \<longleftrightarrow> (\<forall>x y z. R x z \<longrightarrow> R y z \<longrightarrow> x = y)"
kuncar@51994
    41
kuncar@47308
    42
subsection {* Quotient Predicate *}
kuncar@47308
    43
kuncar@47308
    44
definition
kuncar@47308
    45
  "Quotient R Abs Rep T \<longleftrightarrow>
kuncar@47308
    46
     (\<forall>a. Abs (Rep a) = a) \<and> 
kuncar@47308
    47
     (\<forall>a. R (Rep a) (Rep a)) \<and>
kuncar@47308
    48
     (\<forall>r s. R r s \<longleftrightarrow> R r r \<and> R s s \<and> Abs r = Abs s) \<and>
kuncar@47308
    49
     T = (\<lambda>x y. R x x \<and> Abs x = y)"
kuncar@47308
    50
kuncar@47308
    51
lemma QuotientI:
kuncar@47308
    52
  assumes "\<And>a. Abs (Rep a) = a"
kuncar@47308
    53
    and "\<And>a. R (Rep a) (Rep a)"
kuncar@47308
    54
    and "\<And>r s. R r s \<longleftrightarrow> R r r \<and> R s s \<and> Abs r = Abs s"
kuncar@47308
    55
    and "T = (\<lambda>x y. R x x \<and> Abs x = y)"
kuncar@47308
    56
  shows "Quotient R Abs Rep T"
kuncar@47308
    57
  using assms unfolding Quotient_def by blast
kuncar@47308
    58
huffman@47536
    59
context
huffman@47536
    60
  fixes R Abs Rep T
kuncar@47308
    61
  assumes a: "Quotient R Abs Rep T"
huffman@47536
    62
begin
huffman@47536
    63
huffman@47536
    64
lemma Quotient_abs_rep: "Abs (Rep a) = a"
huffman@47536
    65
  using a unfolding Quotient_def
kuncar@47308
    66
  by simp
kuncar@47308
    67
huffman@47536
    68
lemma Quotient_rep_reflp: "R (Rep a) (Rep a)"
huffman@47536
    69
  using a unfolding Quotient_def
kuncar@47308
    70
  by blast
kuncar@47308
    71
kuncar@47308
    72
lemma Quotient_rel:
huffman@47536
    73
  "R r r \<and> R s s \<and> Abs r = Abs s \<longleftrightarrow> R r s" -- {* orientation does not loop on rewriting *}
huffman@47536
    74
  using a unfolding Quotient_def
kuncar@47308
    75
  by blast
kuncar@47308
    76
huffman@47536
    77
lemma Quotient_cr_rel: "T = (\<lambda>x y. R x x \<and> Abs x = y)"
kuncar@47308
    78
  using a unfolding Quotient_def
kuncar@47308
    79
  by blast
kuncar@47308
    80
huffman@47536
    81
lemma Quotient_refl1: "R r s \<Longrightarrow> R r r"
huffman@47536
    82
  using a unfolding Quotient_def
huffman@47536
    83
  by fast
huffman@47536
    84
huffman@47536
    85
lemma Quotient_refl2: "R r s \<Longrightarrow> R s s"
huffman@47536
    86
  using a unfolding Quotient_def
huffman@47536
    87
  by fast
huffman@47536
    88
huffman@47536
    89
lemma Quotient_rel_rep: "R (Rep a) (Rep b) \<longleftrightarrow> a = b"
huffman@47536
    90
  using a unfolding Quotient_def
huffman@47536
    91
  by metis
huffman@47536
    92
huffman@47536
    93
lemma Quotient_rep_abs: "R r r \<Longrightarrow> R (Rep (Abs r)) r"
kuncar@47308
    94
  using a unfolding Quotient_def
kuncar@47308
    95
  by blast
kuncar@47308
    96
kuncar@47937
    97
lemma Quotient_rep_abs_fold_unmap: 
kuncar@47937
    98
  assumes "x' \<equiv> Abs x" and "R x x" and "Rep x' \<equiv> Rep' x'" 
kuncar@47937
    99
  shows "R (Rep' x') x"
kuncar@47937
   100
proof -
kuncar@47937
   101
  have "R (Rep x') x" using assms(1-2) Quotient_rep_abs by auto
kuncar@47937
   102
  then show ?thesis using assms(3) by simp
kuncar@47937
   103
qed
kuncar@47937
   104
kuncar@47937
   105
lemma Quotient_Rep_eq:
kuncar@47937
   106
  assumes "x' \<equiv> Abs x" 
kuncar@47937
   107
  shows "Rep x' \<equiv> Rep x'"
kuncar@47937
   108
by simp
kuncar@47937
   109
huffman@47536
   110
lemma Quotient_rel_abs: "R r s \<Longrightarrow> Abs r = Abs s"
huffman@47536
   111
  using a unfolding Quotient_def
huffman@47536
   112
  by blast
huffman@47536
   113
kuncar@47937
   114
lemma Quotient_rel_abs2:
kuncar@47937
   115
  assumes "R (Rep x) y"
kuncar@47937
   116
  shows "x = Abs y"
kuncar@47937
   117
proof -
kuncar@47937
   118
  from assms have "Abs (Rep x) = Abs y" by (auto intro: Quotient_rel_abs)
kuncar@47937
   119
  then show ?thesis using assms(1) by (simp add: Quotient_abs_rep)
kuncar@47937
   120
qed
kuncar@47937
   121
huffman@47536
   122
lemma Quotient_symp: "symp R"
kuncar@47308
   123
  using a unfolding Quotient_def using sympI by (metis (full_types))
kuncar@47308
   124
huffman@47536
   125
lemma Quotient_transp: "transp R"
kuncar@47308
   126
  using a unfolding Quotient_def using transpI by (metis (full_types))
kuncar@47308
   127
huffman@47536
   128
lemma Quotient_part_equivp: "part_equivp R"
huffman@47536
   129
by (metis Quotient_rep_reflp Quotient_symp Quotient_transp part_equivpI)
huffman@47536
   130
huffman@47536
   131
end
kuncar@47308
   132
kuncar@47308
   133
lemma identity_quotient: "Quotient (op =) id id (op =)"
kuncar@47308
   134
unfolding Quotient_def by simp 
kuncar@47308
   135
huffman@47652
   136
text {* TODO: Use one of these alternatives as the real definition. *}
huffman@47652
   137
kuncar@47308
   138
lemma Quotient_alt_def:
kuncar@47308
   139
  "Quotient R Abs Rep T \<longleftrightarrow>
kuncar@47308
   140
    (\<forall>a b. T a b \<longrightarrow> Abs a = b) \<and>
kuncar@47308
   141
    (\<forall>b. T (Rep b) b) \<and>
kuncar@47308
   142
    (\<forall>x y. R x y \<longleftrightarrow> T x (Abs x) \<and> T y (Abs y) \<and> Abs x = Abs y)"
kuncar@47308
   143
apply safe
kuncar@47308
   144
apply (simp (no_asm_use) only: Quotient_def, fast)
kuncar@47308
   145
apply (simp (no_asm_use) only: Quotient_def, fast)
kuncar@47308
   146
apply (simp (no_asm_use) only: Quotient_def, fast)
kuncar@47308
   147
apply (simp (no_asm_use) only: Quotient_def, fast)
kuncar@47308
   148
apply (simp (no_asm_use) only: Quotient_def, fast)
kuncar@47308
   149
apply (simp (no_asm_use) only: Quotient_def, fast)
kuncar@47308
   150
apply (rule QuotientI)
kuncar@47308
   151
apply simp
kuncar@47308
   152
apply metis
kuncar@47308
   153
apply simp
kuncar@47308
   154
apply (rule ext, rule ext, metis)
kuncar@47308
   155
done
kuncar@47308
   156
kuncar@47308
   157
lemma Quotient_alt_def2:
kuncar@47308
   158
  "Quotient R Abs Rep T \<longleftrightarrow>
kuncar@47308
   159
    (\<forall>a b. T a b \<longrightarrow> Abs a = b) \<and>
kuncar@47308
   160
    (\<forall>b. T (Rep b) b) \<and>
kuncar@47308
   161
    (\<forall>x y. R x y \<longleftrightarrow> T x (Abs y) \<and> T y (Abs x))"
kuncar@47308
   162
  unfolding Quotient_alt_def by (safe, metis+)
kuncar@47308
   163
huffman@47652
   164
lemma Quotient_alt_def3:
huffman@47652
   165
  "Quotient R Abs Rep T \<longleftrightarrow>
huffman@47652
   166
    (\<forall>a b. T a b \<longrightarrow> Abs a = b) \<and> (\<forall>b. T (Rep b) b) \<and>
huffman@47652
   167
    (\<forall>x y. R x y \<longleftrightarrow> (\<exists>z. T x z \<and> T y z))"
huffman@47652
   168
  unfolding Quotient_alt_def2 by (safe, metis+)
huffman@47652
   169
huffman@47652
   170
lemma Quotient_alt_def4:
huffman@47652
   171
  "Quotient R Abs Rep T \<longleftrightarrow>
huffman@47652
   172
    (\<forall>a b. T a b \<longrightarrow> Abs a = b) \<and> (\<forall>b. T (Rep b) b) \<and> R = T OO conversep T"
huffman@47652
   173
  unfolding Quotient_alt_def3 fun_eq_iff by auto
huffman@47652
   174
kuncar@47308
   175
lemma fun_quotient:
kuncar@47308
   176
  assumes 1: "Quotient R1 abs1 rep1 T1"
kuncar@47308
   177
  assumes 2: "Quotient R2 abs2 rep2 T2"
kuncar@47308
   178
  shows "Quotient (R1 ===> R2) (rep1 ---> abs2) (abs1 ---> rep2) (T1 ===> T2)"
kuncar@47308
   179
  using assms unfolding Quotient_alt_def2
kuncar@47308
   180
  unfolding fun_rel_def fun_eq_iff map_fun_apply
kuncar@47308
   181
  by (safe, metis+)
kuncar@47308
   182
kuncar@47308
   183
lemma apply_rsp:
kuncar@47308
   184
  fixes f g::"'a \<Rightarrow> 'c"
kuncar@47308
   185
  assumes q: "Quotient R1 Abs1 Rep1 T1"
kuncar@47308
   186
  and     a: "(R1 ===> R2) f g" "R1 x y"
kuncar@47308
   187
  shows "R2 (f x) (g y)"
kuncar@47308
   188
  using a by (auto elim: fun_relE)
kuncar@47308
   189
kuncar@47308
   190
lemma apply_rsp':
kuncar@47308
   191
  assumes a: "(R1 ===> R2) f g" "R1 x y"
kuncar@47308
   192
  shows "R2 (f x) (g y)"
kuncar@47308
   193
  using a by (auto elim: fun_relE)
kuncar@47308
   194
kuncar@47308
   195
lemma apply_rsp'':
kuncar@47308
   196
  assumes "Quotient R Abs Rep T"
kuncar@47308
   197
  and "(R ===> S) f f"
kuncar@47308
   198
  shows "S (f (Rep x)) (f (Rep x))"
kuncar@47308
   199
proof -
kuncar@47308
   200
  from assms(1) have "R (Rep x) (Rep x)" by (rule Quotient_rep_reflp)
kuncar@47308
   201
  then show ?thesis using assms(2) by (auto intro: apply_rsp')
kuncar@47308
   202
qed
kuncar@47308
   203
kuncar@47308
   204
subsection {* Quotient composition *}
kuncar@47308
   205
kuncar@47308
   206
lemma Quotient_compose:
kuncar@47308
   207
  assumes 1: "Quotient R1 Abs1 Rep1 T1"
kuncar@47308
   208
  assumes 2: "Quotient R2 Abs2 Rep2 T2"
kuncar@47308
   209
  shows "Quotient (T1 OO R2 OO conversep T1) (Abs2 \<circ> Abs1) (Rep1 \<circ> Rep2) (T1 OO T2)"
kuncar@51994
   210
  using assms unfolding Quotient_alt_def4 by fastforce
kuncar@47308
   211
kuncar@47521
   212
lemma equivp_reflp2:
kuncar@47521
   213
  "equivp R \<Longrightarrow> reflp R"
kuncar@47521
   214
  by (erule equivpE)
kuncar@47521
   215
huffman@47544
   216
subsection {* Respects predicate *}
huffman@47544
   217
huffman@47544
   218
definition Respects :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a set"
huffman@47544
   219
  where "Respects R = {x. R x x}"
huffman@47544
   220
huffman@47544
   221
lemma in_respects: "x \<in> Respects R \<longleftrightarrow> R x x"
huffman@47544
   222
  unfolding Respects_def by simp
huffman@47544
   223
kuncar@47308
   224
subsection {* Invariant *}
kuncar@47308
   225
kuncar@47308
   226
definition invariant :: "('a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool" 
kuncar@47308
   227
  where "invariant R = (\<lambda>x y. R x \<and> x = y)"
kuncar@47308
   228
kuncar@47308
   229
lemma invariant_to_eq:
kuncar@47308
   230
  assumes "invariant P x y"
kuncar@47308
   231
  shows "x = y"
kuncar@47308
   232
using assms by (simp add: invariant_def)
kuncar@47308
   233
kuncar@47308
   234
lemma fun_rel_eq_invariant:
kuncar@47308
   235
  shows "((invariant R) ===> S) = (\<lambda>f g. \<forall>x. R x \<longrightarrow> S (f x) (g x))"
kuncar@47308
   236
by (auto simp add: invariant_def fun_rel_def)
kuncar@47308
   237
kuncar@47308
   238
lemma invariant_same_args:
kuncar@47308
   239
  shows "invariant P x x \<equiv> P x"
kuncar@47308
   240
using assms by (auto simp add: invariant_def)
kuncar@47308
   241
kuncar@47361
   242
lemma UNIV_typedef_to_Quotient:
kuncar@47308
   243
  assumes "type_definition Rep Abs UNIV"
kuncar@47361
   244
  and T_def: "T \<equiv> (\<lambda>x y. x = Rep y)"
kuncar@47308
   245
  shows "Quotient (op =) Abs Rep T"
kuncar@47308
   246
proof -
kuncar@47308
   247
  interpret type_definition Rep Abs UNIV by fact
kuncar@47361
   248
  from Abs_inject Rep_inverse Abs_inverse T_def show ?thesis 
kuncar@47361
   249
    by (fastforce intro!: QuotientI fun_eq_iff)
kuncar@47308
   250
qed
kuncar@47308
   251
kuncar@47361
   252
lemma UNIV_typedef_to_equivp:
kuncar@47308
   253
  fixes Abs :: "'a \<Rightarrow> 'b"
kuncar@47308
   254
  and Rep :: "'b \<Rightarrow> 'a"
kuncar@47308
   255
  assumes "type_definition Rep Abs (UNIV::'a set)"
kuncar@47308
   256
  shows "equivp (op=::'a\<Rightarrow>'a\<Rightarrow>bool)"
kuncar@47308
   257
by (rule identity_equivp)
kuncar@47308
   258
huffman@47354
   259
lemma typedef_to_Quotient:
kuncar@47361
   260
  assumes "type_definition Rep Abs S"
kuncar@47361
   261
  and T_def: "T \<equiv> (\<lambda>x y. x = Rep y)"
kuncar@47501
   262
  shows "Quotient (invariant (\<lambda>x. x \<in> S)) Abs Rep T"
kuncar@47361
   263
proof -
kuncar@47361
   264
  interpret type_definition Rep Abs S by fact
kuncar@47361
   265
  from Rep Abs_inject Rep_inverse Abs_inverse T_def show ?thesis
kuncar@47361
   266
    by (auto intro!: QuotientI simp: invariant_def fun_eq_iff)
kuncar@47361
   267
qed
kuncar@47361
   268
kuncar@47361
   269
lemma typedef_to_part_equivp:
kuncar@47361
   270
  assumes "type_definition Rep Abs S"
kuncar@47501
   271
  shows "part_equivp (invariant (\<lambda>x. x \<in> S))"
kuncar@47361
   272
proof (intro part_equivpI)
kuncar@47361
   273
  interpret type_definition Rep Abs S by fact
kuncar@47501
   274
  show "\<exists>x. invariant (\<lambda>x. x \<in> S) x x" using Rep by (auto simp: invariant_def)
kuncar@47361
   275
next
kuncar@47501
   276
  show "symp (invariant (\<lambda>x. x \<in> S))" by (auto intro: sympI simp: invariant_def)
kuncar@47361
   277
next
kuncar@47501
   278
  show "transp (invariant (\<lambda>x. x \<in> S))" by (auto intro: transpI simp: invariant_def)
kuncar@47361
   279
qed
kuncar@47361
   280
kuncar@47361
   281
lemma open_typedef_to_Quotient:
kuncar@47308
   282
  assumes "type_definition Rep Abs {x. P x}"
huffman@47354
   283
  and T_def: "T \<equiv> (\<lambda>x y. x = Rep y)"
kuncar@47308
   284
  shows "Quotient (invariant P) Abs Rep T"
huffman@47651
   285
  using typedef_to_Quotient [OF assms] by simp
kuncar@47308
   286
kuncar@47361
   287
lemma open_typedef_to_part_equivp:
kuncar@47308
   288
  assumes "type_definition Rep Abs {x. P x}"
kuncar@47308
   289
  shows "part_equivp (invariant P)"
huffman@47651
   290
  using typedef_to_part_equivp [OF assms] by simp
kuncar@47308
   291
huffman@47376
   292
text {* Generating transfer rules for quotients. *}
huffman@47376
   293
huffman@47537
   294
context
huffman@47537
   295
  fixes R Abs Rep T
huffman@47537
   296
  assumes 1: "Quotient R Abs Rep T"
huffman@47537
   297
begin
huffman@47376
   298
huffman@47537
   299
lemma Quotient_right_unique: "right_unique T"
huffman@47537
   300
  using 1 unfolding Quotient_alt_def right_unique_def by metis
huffman@47537
   301
huffman@47537
   302
lemma Quotient_right_total: "right_total T"
huffman@47537
   303
  using 1 unfolding Quotient_alt_def right_total_def by metis
huffman@47537
   304
huffman@47537
   305
lemma Quotient_rel_eq_transfer: "(T ===> T ===> op =) R (op =)"
huffman@47537
   306
  using 1 unfolding Quotient_alt_def fun_rel_def by simp
huffman@47376
   307
huffman@47538
   308
lemma Quotient_abs_induct:
huffman@47538
   309
  assumes "\<And>y. R y y \<Longrightarrow> P (Abs y)" shows "P x"
huffman@47538
   310
  using 1 assms unfolding Quotient_def by metis
huffman@47538
   311
huffman@47537
   312
end
huffman@47537
   313
huffman@47537
   314
text {* Generating transfer rules for total quotients. *}
huffman@47376
   315
huffman@47537
   316
context
huffman@47537
   317
  fixes R Abs Rep T
huffman@47537
   318
  assumes 1: "Quotient R Abs Rep T" and 2: "reflp R"
huffman@47537
   319
begin
huffman@47376
   320
huffman@47537
   321
lemma Quotient_bi_total: "bi_total T"
huffman@47537
   322
  using 1 2 unfolding Quotient_alt_def bi_total_def reflp_def by auto
huffman@47537
   323
huffman@47537
   324
lemma Quotient_id_abs_transfer: "(op = ===> T) (\<lambda>x. x) Abs"
huffman@47537
   325
  using 1 2 unfolding Quotient_alt_def reflp_def fun_rel_def by simp
huffman@47537
   326
huffman@47575
   327
lemma Quotient_total_abs_induct: "(\<And>y. P (Abs y)) \<Longrightarrow> P x"
huffman@47575
   328
  using 1 2 assms unfolding Quotient_alt_def reflp_def by metis
huffman@47575
   329
huffman@47889
   330
lemma Quotient_total_abs_eq_iff: "Abs x = Abs y \<longleftrightarrow> R x y"
huffman@47889
   331
  using Quotient_rel [OF 1] 2 unfolding reflp_def by simp
huffman@47889
   332
huffman@47537
   333
end
huffman@47376
   334
huffman@47368
   335
text {* Generating transfer rules for a type defined with @{text "typedef"}. *}
huffman@47368
   336
huffman@47534
   337
context
huffman@47534
   338
  fixes Rep Abs A T
huffman@47368
   339
  assumes type: "type_definition Rep Abs A"
huffman@47534
   340
  assumes T_def: "T \<equiv> (\<lambda>(x::'a) (y::'b). x = Rep y)"
huffman@47534
   341
begin
huffman@47534
   342
kuncar@51994
   343
lemma typedef_left_unique: "left_unique T"
kuncar@51994
   344
  unfolding left_unique_def T_def
kuncar@51994
   345
  by (simp add: type_definition.Rep_inject [OF type])
kuncar@51994
   346
huffman@47534
   347
lemma typedef_bi_unique: "bi_unique T"
huffman@47368
   348
  unfolding bi_unique_def T_def
huffman@47368
   349
  by (simp add: type_definition.Rep_inject [OF type])
huffman@47368
   350
kuncar@51374
   351
(* the following two theorems are here only for convinience *)
kuncar@51374
   352
kuncar@51374
   353
lemma typedef_right_unique: "right_unique T"
kuncar@51374
   354
  using T_def type Quotient_right_unique typedef_to_Quotient 
kuncar@51374
   355
  by blast
kuncar@51374
   356
kuncar@51374
   357
lemma typedef_right_total: "right_total T"
kuncar@51374
   358
  using T_def type Quotient_right_total typedef_to_Quotient 
kuncar@51374
   359
  by blast
kuncar@51374
   360
huffman@47535
   361
lemma typedef_rep_transfer: "(T ===> op =) (\<lambda>x. x) Rep"
huffman@47535
   362
  unfolding fun_rel_def T_def by simp
huffman@47535
   363
huffman@47534
   364
end
huffman@47534
   365
huffman@47368
   366
text {* Generating the correspondence rule for a constant defined with
huffman@47368
   367
  @{text "lift_definition"}. *}
huffman@47368
   368
huffman@47351
   369
lemma Quotient_to_transfer:
huffman@47351
   370
  assumes "Quotient R Abs Rep T" and "R c c" and "c' \<equiv> Abs c"
huffman@47351
   371
  shows "T c c'"
huffman@47351
   372
  using assms by (auto dest: Quotient_cr_rel)
huffman@47351
   373
kuncar@47982
   374
text {* Proving reflexivity *}
kuncar@47982
   375
kuncar@51994
   376
definition reflp' :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool" where "reflp' R \<equiv> reflp R"
kuncar@47982
   377
kuncar@47982
   378
lemma Quotient_to_left_total:
kuncar@47982
   379
  assumes q: "Quotient R Abs Rep T"
kuncar@47982
   380
  and r_R: "reflp R"
kuncar@47982
   381
  shows "left_total T"
kuncar@47982
   382
using r_R Quotient_cr_rel[OF q] unfolding left_total_def by (auto elim: reflpE)
kuncar@47982
   383
kuncar@47982
   384
lemma reflp_Quotient_composition:
kuncar@51994
   385
  assumes "left_total R"
kuncar@51994
   386
  assumes "reflp T"
kuncar@51994
   387
  shows "reflp (R OO T OO R\<inverse>\<inverse>)"
kuncar@51994
   388
using assms unfolding reflp_def left_total_def by fast
kuncar@51994
   389
kuncar@51994
   390
lemma reflp_fun1:
kuncar@51994
   391
  assumes "is_equality R"
kuncar@51994
   392
  assumes "reflp' S"
kuncar@51994
   393
  shows "reflp (R ===> S)"
kuncar@51994
   394
using assms unfolding is_equality_def reflp'_def reflp_def fun_rel_def by blast
kuncar@51994
   395
kuncar@51994
   396
lemma reflp_fun2:
kuncar@51994
   397
  assumes "is_equality R"
kuncar@51994
   398
  assumes "is_equality S"
kuncar@51994
   399
  shows "reflp (R ===> S)"
kuncar@51994
   400
using assms unfolding is_equality_def reflp_def fun_rel_def by blast
kuncar@51994
   401
kuncar@51994
   402
lemma is_equality_Quotient_composition:
kuncar@51994
   403
  assumes "is_equality T"
kuncar@51994
   404
  assumes "left_total R"
kuncar@51994
   405
  assumes "left_unique R"
kuncar@51994
   406
  shows "is_equality (R OO T OO R\<inverse>\<inverse>)"
kuncar@51994
   407
using assms unfolding is_equality_def left_total_def left_unique_def OO_def conversep_iff
kuncar@51994
   408
by fastforce
kuncar@47982
   409
kuncar@47982
   410
lemma reflp_equality: "reflp (op =)"
kuncar@47982
   411
by (auto intro: reflpI)
kuncar@47982
   412
kuncar@51374
   413
text {* Proving a parametrized correspondence relation *}
kuncar@51374
   414
kuncar@51374
   415
lemma eq_OO: "op= OO R = R"
kuncar@51374
   416
unfolding OO_def by metis
kuncar@51374
   417
kuncar@51374
   418
definition POS :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool" where
kuncar@51374
   419
"POS A B \<equiv> A \<le> B"
kuncar@51374
   420
kuncar@51374
   421
definition  NEG :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool" where
kuncar@51374
   422
"NEG A B \<equiv> B \<le> A"
kuncar@51374
   423
kuncar@51374
   424
(*
kuncar@51374
   425
  The following two rules are here because we don't have any proper
kuncar@51374
   426
  left-unique ant left-total relations. Left-unique and left-total
kuncar@51374
   427
  assumptions show up in distributivity rules for the function type.
kuncar@51374
   428
*)
kuncar@51374
   429
kuncar@51374
   430
lemma bi_unique_left_unique[transfer_rule]: "bi_unique R \<Longrightarrow> left_unique R"
kuncar@51374
   431
unfolding bi_unique_def left_unique_def by blast
kuncar@51374
   432
kuncar@51374
   433
lemma bi_total_left_total[transfer_rule]: "bi_total R \<Longrightarrow> left_total R"
kuncar@51374
   434
unfolding bi_total_def left_total_def by blast
kuncar@51374
   435
kuncar@51374
   436
lemma pos_OO_eq:
kuncar@51374
   437
  shows "POS (A OO op=) A"
kuncar@51374
   438
unfolding POS_def OO_def by blast
kuncar@51374
   439
kuncar@51374
   440
lemma pos_eq_OO:
kuncar@51374
   441
  shows "POS (op= OO A) A"
kuncar@51374
   442
unfolding POS_def OO_def by blast
kuncar@51374
   443
kuncar@51374
   444
lemma neg_OO_eq:
kuncar@51374
   445
  shows "NEG (A OO op=) A"
kuncar@51374
   446
unfolding NEG_def OO_def by auto
kuncar@51374
   447
kuncar@51374
   448
lemma neg_eq_OO:
kuncar@51374
   449
  shows "NEG (op= OO A) A"
kuncar@51374
   450
unfolding NEG_def OO_def by blast
kuncar@51374
   451
kuncar@51374
   452
lemma POS_trans:
kuncar@51374
   453
  assumes "POS A B"
kuncar@51374
   454
  assumes "POS B C"
kuncar@51374
   455
  shows "POS A C"
kuncar@51374
   456
using assms unfolding POS_def by auto
kuncar@51374
   457
kuncar@51374
   458
lemma NEG_trans:
kuncar@51374
   459
  assumes "NEG A B"
kuncar@51374
   460
  assumes "NEG B C"
kuncar@51374
   461
  shows "NEG A C"
kuncar@51374
   462
using assms unfolding NEG_def by auto
kuncar@51374
   463
kuncar@51374
   464
lemma POS_NEG:
kuncar@51374
   465
  "POS A B \<equiv> NEG B A"
kuncar@51374
   466
  unfolding POS_def NEG_def by auto
kuncar@51374
   467
kuncar@51374
   468
lemma NEG_POS:
kuncar@51374
   469
  "NEG A B \<equiv> POS B A"
kuncar@51374
   470
  unfolding POS_def NEG_def by auto
kuncar@51374
   471
kuncar@51374
   472
lemma POS_pcr_rule:
kuncar@51374
   473
  assumes "POS (A OO B) C"
kuncar@51374
   474
  shows "POS (A OO B OO X) (C OO X)"
kuncar@51374
   475
using assms unfolding POS_def OO_def by blast
kuncar@51374
   476
kuncar@51374
   477
lemma NEG_pcr_rule:
kuncar@51374
   478
  assumes "NEG (A OO B) C"
kuncar@51374
   479
  shows "NEG (A OO B OO X) (C OO X)"
kuncar@51374
   480
using assms unfolding NEG_def OO_def by blast
kuncar@51374
   481
kuncar@51374
   482
lemma POS_apply:
kuncar@51374
   483
  assumes "POS R R'"
kuncar@51374
   484
  assumes "R f g"
kuncar@51374
   485
  shows "R' f g"
kuncar@51374
   486
using assms unfolding POS_def by auto
kuncar@51374
   487
kuncar@51374
   488
text {* Proving a parametrized correspondence relation *}
kuncar@51374
   489
kuncar@51374
   490
lemma fun_mono:
kuncar@51374
   491
  assumes "A \<ge> C"
kuncar@51374
   492
  assumes "B \<le> D"
kuncar@51374
   493
  shows   "(A ===> B) \<le> (C ===> D)"
kuncar@51374
   494
using assms unfolding fun_rel_def by blast
kuncar@51374
   495
kuncar@51374
   496
lemma pos_fun_distr: "((R ===> S) OO (R' ===> S')) \<le> ((R OO R') ===> (S OO S'))"
kuncar@51374
   497
unfolding OO_def fun_rel_def by blast
kuncar@51374
   498
kuncar@51374
   499
lemma functional_relation: "right_unique R \<Longrightarrow> left_total R \<Longrightarrow> \<forall>x. \<exists>!y. R x y"
kuncar@51374
   500
unfolding right_unique_def left_total_def by blast
kuncar@51374
   501
kuncar@51374
   502
lemma functional_converse_relation: "left_unique R \<Longrightarrow> right_total R \<Longrightarrow> \<forall>y. \<exists>!x. R x y"
kuncar@51374
   503
unfolding left_unique_def right_total_def by blast
kuncar@51374
   504
kuncar@51374
   505
lemma neg_fun_distr1:
kuncar@51374
   506
assumes 1: "left_unique R" "right_total R"
kuncar@51374
   507
assumes 2: "right_unique R'" "left_total R'"
kuncar@51374
   508
shows "(R OO R' ===> S OO S') \<le> ((R ===> S) OO (R' ===> S')) "
kuncar@51374
   509
  using functional_relation[OF 2] functional_converse_relation[OF 1]
kuncar@51374
   510
  unfolding fun_rel_def OO_def
kuncar@51374
   511
  apply clarify
kuncar@51374
   512
  apply (subst all_comm)
kuncar@51374
   513
  apply (subst all_conj_distrib[symmetric])
kuncar@51374
   514
  apply (intro choice)
kuncar@51374
   515
  by metis
kuncar@51374
   516
kuncar@51374
   517
lemma neg_fun_distr2:
kuncar@51374
   518
assumes 1: "right_unique R'" "left_total R'"
kuncar@51374
   519
assumes 2: "left_unique S'" "right_total S'"
kuncar@51374
   520
shows "(R OO R' ===> S OO S') \<le> ((R ===> S) OO (R' ===> S'))"
kuncar@51374
   521
  using functional_converse_relation[OF 2] functional_relation[OF 1]
kuncar@51374
   522
  unfolding fun_rel_def OO_def
kuncar@51374
   523
  apply clarify
kuncar@51374
   524
  apply (subst all_comm)
kuncar@51374
   525
  apply (subst all_conj_distrib[symmetric])
kuncar@51374
   526
  apply (intro choice)
kuncar@51374
   527
  by metis
kuncar@51374
   528
kuncar@51956
   529
subsection {* Domains *}
kuncar@51956
   530
kuncar@51956
   531
lemma pcr_Domainp_par_left_total:
kuncar@51956
   532
  assumes "Domainp B = P"
kuncar@51956
   533
  assumes "left_total A"
kuncar@51956
   534
  assumes "(A ===> op=) P' P"
kuncar@51956
   535
  shows "Domainp (A OO B) = P'"
kuncar@51956
   536
using assms
kuncar@51956
   537
unfolding Domainp_iff[abs_def] OO_def bi_unique_def left_total_def fun_rel_def 
kuncar@51956
   538
by (fast intro: fun_eq_iff)
kuncar@51956
   539
kuncar@51956
   540
lemma pcr_Domainp_par:
kuncar@51956
   541
assumes "Domainp B = P2"
kuncar@51956
   542
assumes "Domainp A = P1"
kuncar@51956
   543
assumes "(A ===> op=) P2' P2"
kuncar@51956
   544
shows "Domainp (A OO B) = (inf P1 P2')"
kuncar@51956
   545
using assms unfolding fun_rel_def Domainp_iff[abs_def] OO_def
kuncar@51956
   546
by (fast intro: fun_eq_iff)
kuncar@51956
   547
kuncar@51956
   548
definition rel_pred_comp :: "('a => 'b => bool) => ('b => bool) => 'a => bool"  (infixr "OP" 75)
kuncar@51956
   549
where "rel_pred_comp R P \<equiv> \<lambda>x. \<exists>y. R x y \<and> P y"
kuncar@51956
   550
kuncar@51956
   551
lemma pcr_Domainp:
kuncar@51956
   552
assumes "Domainp B = P"
kuncar@51956
   553
shows "Domainp (A OO B) = (A OP P)"
kuncar@51956
   554
using assms unfolding rel_pred_comp_def by blast
kuncar@51956
   555
kuncar@51956
   556
lemma pcr_Domainp_total:
kuncar@51956
   557
  assumes "bi_total B"
kuncar@51956
   558
  assumes "Domainp A = P"
kuncar@51956
   559
  shows "Domainp (A OO B) = P"
kuncar@51956
   560
using assms unfolding bi_total_def 
kuncar@51956
   561
by fast
kuncar@51956
   562
kuncar@51956
   563
lemma Quotient_to_Domainp:
kuncar@51956
   564
  assumes "Quotient R Abs Rep T"
kuncar@51956
   565
  shows "Domainp T = (\<lambda>x. R x x)"  
kuncar@51956
   566
by (simp add: Domainp_iff[abs_def] Quotient_cr_rel[OF assms])
kuncar@51956
   567
kuncar@51956
   568
lemma invariant_to_Domainp:
kuncar@51956
   569
  assumes "Quotient (Lifting.invariant P) Abs Rep T"
kuncar@51956
   570
  shows "Domainp T = P"
kuncar@51956
   571
by (simp add: invariant_def Domainp_iff[abs_def] Quotient_cr_rel[OF assms])
kuncar@51956
   572
kuncar@47308
   573
subsection {* ML setup *}
kuncar@47308
   574
wenzelm@48891
   575
ML_file "Tools/Lifting/lifting_util.ML"
kuncar@47308
   576
wenzelm@48891
   577
ML_file "Tools/Lifting/lifting_info.ML"
kuncar@47308
   578
setup Lifting_Info.setup
kuncar@47308
   579
kuncar@51994
   580
lemmas [reflexivity_rule] = 
kuncar@51994
   581
  reflp_equality reflp_Quotient_composition is_equality_Quotient_composition
kuncar@51994
   582
kuncar@51994
   583
text {* add @{thm reflp_fun1} and @{thm reflp_fun2} manually through ML
kuncar@51994
   584
  because we don't want to get reflp' variant of these theorems *}
kuncar@51994
   585
kuncar@51994
   586
setup{*
kuncar@51994
   587
Context.theory_map 
kuncar@51994
   588
  (fold
kuncar@51994
   589
    (snd oo (Thm.apply_attribute Lifting_Info.add_reflexivity_rule_raw_attribute)) 
kuncar@51994
   590
      [@{thm reflp_fun1}, @{thm reflp_fun2}])
kuncar@51994
   591
*}
kuncar@51374
   592
kuncar@51374
   593
(* setup for the function type *)
kuncar@47777
   594
declare fun_quotient[quot_map]
kuncar@51374
   595
declare fun_mono[relator_mono]
kuncar@51374
   596
lemmas [relator_distr] = pos_fun_distr neg_fun_distr1 neg_fun_distr2
kuncar@47308
   597
wenzelm@48891
   598
ML_file "Tools/Lifting/lifting_term.ML"
kuncar@47308
   599
wenzelm@48891
   600
ML_file "Tools/Lifting/lifting_def.ML"
kuncar@47308
   601
wenzelm@48891
   602
ML_file "Tools/Lifting/lifting_setup.ML"
kuncar@47308
   603
kuncar@51994
   604
hide_const (open) invariant POS NEG reflp'
kuncar@47308
   605
kuncar@47308
   606
end