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