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