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