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