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