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