src/HOL/Lifting.thy
author blanchet
Mon Jan 20 20:21:12 2014 +0100 (2014-01-20)
changeset 55083 0a689157e3ce
parent 53952 b2781a3ce958
child 55563 a64d49f49ca3
permissions -rw-r--r--
move BNF_LFP up the dependency chain
     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_fold_unmap: 
   159   assumes "x' \<equiv> Abs x" and "R x x" and "Rep x' \<equiv> Rep' x'" 
   160   shows "R (Rep' x') x"
   161 proof -
   162   have "R (Rep x') x" using assms(1-2) Quotient_rep_abs by auto
   163   then show ?thesis using assms(3) by simp
   164 qed
   165 
   166 lemma Quotient_Rep_eq:
   167   assumes "x' \<equiv> Abs x" 
   168   shows "Rep x' \<equiv> Rep x'"
   169 by simp
   170 
   171 lemma Quotient_rel_abs: "R r s \<Longrightarrow> Abs r = Abs s"
   172   using a unfolding Quotient_def
   173   by blast
   174 
   175 lemma Quotient_rel_abs2:
   176   assumes "R (Rep x) y"
   177   shows "x = Abs y"
   178 proof -
   179   from assms have "Abs (Rep x) = Abs y" by (auto intro: Quotient_rel_abs)
   180   then show ?thesis using assms(1) by (simp add: Quotient_abs_rep)
   181 qed
   182 
   183 lemma Quotient_symp: "symp R"
   184   using a unfolding Quotient_def using sympI by (metis (full_types))
   185 
   186 lemma Quotient_transp: "transp R"
   187   using a unfolding Quotient_def using transpI by (metis (full_types))
   188 
   189 lemma Quotient_part_equivp: "part_equivp R"
   190 by (metis Quotient_rep_reflp Quotient_symp Quotient_transp part_equivpI)
   191 
   192 end
   193 
   194 lemma identity_quotient: "Quotient (op =) id id (op =)"
   195 unfolding Quotient_def by simp 
   196 
   197 text {* TODO: Use one of these alternatives as the real definition. *}
   198 
   199 lemma Quotient_alt_def:
   200   "Quotient R Abs Rep T \<longleftrightarrow>
   201     (\<forall>a b. T a b \<longrightarrow> Abs a = b) \<and>
   202     (\<forall>b. T (Rep b) b) \<and>
   203     (\<forall>x y. R x y \<longleftrightarrow> T x (Abs x) \<and> T y (Abs y) \<and> Abs x = Abs y)"
   204 apply safe
   205 apply (simp (no_asm_use) only: Quotient_def, fast)
   206 apply (simp (no_asm_use) only: Quotient_def, fast)
   207 apply (simp (no_asm_use) only: Quotient_def, fast)
   208 apply (simp (no_asm_use) only: Quotient_def, fast)
   209 apply (simp (no_asm_use) only: Quotient_def, fast)
   210 apply (simp (no_asm_use) only: Quotient_def, fast)
   211 apply (rule QuotientI)
   212 apply simp
   213 apply metis
   214 apply simp
   215 apply (rule ext, rule ext, metis)
   216 done
   217 
   218 lemma Quotient_alt_def2:
   219   "Quotient R Abs Rep T \<longleftrightarrow>
   220     (\<forall>a b. T a b \<longrightarrow> Abs a = b) \<and>
   221     (\<forall>b. T (Rep b) b) \<and>
   222     (\<forall>x y. R x y \<longleftrightarrow> T x (Abs y) \<and> T y (Abs x))"
   223   unfolding Quotient_alt_def by (safe, metis+)
   224 
   225 lemma Quotient_alt_def3:
   226   "Quotient R Abs Rep T \<longleftrightarrow>
   227     (\<forall>a b. T a b \<longrightarrow> Abs a = b) \<and> (\<forall>b. T (Rep b) b) \<and>
   228     (\<forall>x y. R x y \<longleftrightarrow> (\<exists>z. T x z \<and> T y z))"
   229   unfolding Quotient_alt_def2 by (safe, metis+)
   230 
   231 lemma Quotient_alt_def4:
   232   "Quotient R Abs Rep T \<longleftrightarrow>
   233     (\<forall>a b. T a b \<longrightarrow> Abs a = b) \<and> (\<forall>b. T (Rep b) b) \<and> R = T OO conversep T"
   234   unfolding Quotient_alt_def3 fun_eq_iff by auto
   235 
   236 lemma fun_quotient:
   237   assumes 1: "Quotient R1 abs1 rep1 T1"
   238   assumes 2: "Quotient R2 abs2 rep2 T2"
   239   shows "Quotient (R1 ===> R2) (rep1 ---> abs2) (abs1 ---> rep2) (T1 ===> T2)"
   240   using assms unfolding Quotient_alt_def2
   241   unfolding fun_rel_def fun_eq_iff map_fun_apply
   242   by (safe, metis+)
   243 
   244 lemma apply_rsp:
   245   fixes f g::"'a \<Rightarrow> 'c"
   246   assumes q: "Quotient R1 Abs1 Rep1 T1"
   247   and     a: "(R1 ===> R2) f g" "R1 x y"
   248   shows "R2 (f x) (g y)"
   249   using a by (auto elim: fun_relE)
   250 
   251 lemma apply_rsp':
   252   assumes a: "(R1 ===> R2) f g" "R1 x y"
   253   shows "R2 (f x) (g y)"
   254   using a by (auto elim: fun_relE)
   255 
   256 lemma apply_rsp'':
   257   assumes "Quotient R Abs Rep T"
   258   and "(R ===> S) f f"
   259   shows "S (f (Rep x)) (f (Rep x))"
   260 proof -
   261   from assms(1) have "R (Rep x) (Rep x)" by (rule Quotient_rep_reflp)
   262   then show ?thesis using assms(2) by (auto intro: apply_rsp')
   263 qed
   264 
   265 subsection {* Quotient composition *}
   266 
   267 lemma Quotient_compose:
   268   assumes 1: "Quotient R1 Abs1 Rep1 T1"
   269   assumes 2: "Quotient R2 Abs2 Rep2 T2"
   270   shows "Quotient (T1 OO R2 OO conversep T1) (Abs2 \<circ> Abs1) (Rep1 \<circ> Rep2) (T1 OO T2)"
   271   using assms unfolding Quotient_alt_def4 by fastforce
   272 
   273 lemma equivp_reflp2:
   274   "equivp R \<Longrightarrow> reflp R"
   275   by (erule equivpE)
   276 
   277 subsection {* Respects predicate *}
   278 
   279 definition Respects :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a set"
   280   where "Respects R = {x. R x x}"
   281 
   282 lemma in_respects: "x \<in> Respects R \<longleftrightarrow> R x x"
   283   unfolding Respects_def by simp
   284 
   285 subsection {* Invariant *}
   286 
   287 definition invariant :: "('a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool" 
   288   where "invariant R = (\<lambda>x y. R x \<and> x = y)"
   289 
   290 lemma invariant_to_eq:
   291   assumes "invariant P x y"
   292   shows "x = y"
   293 using assms by (simp add: invariant_def)
   294 
   295 lemma fun_rel_eq_invariant:
   296   shows "((invariant R) ===> S) = (\<lambda>f g. \<forall>x. R x \<longrightarrow> S (f x) (g x))"
   297 by (auto simp add: invariant_def fun_rel_def)
   298 
   299 lemma invariant_same_args:
   300   shows "invariant P x x \<equiv> P x"
   301 using assms by (auto simp add: invariant_def)
   302 
   303 lemma invariant_transfer [transfer_rule]:
   304   assumes [transfer_rule]: "bi_unique A"
   305   shows "((A ===> op=) ===> A ===> A ===> op=) Lifting.invariant Lifting.invariant"
   306 unfolding invariant_def[abs_def] by transfer_prover
   307 
   308 lemma UNIV_typedef_to_Quotient:
   309   assumes "type_definition Rep Abs UNIV"
   310   and T_def: "T \<equiv> (\<lambda>x y. x = Rep y)"
   311   shows "Quotient (op =) Abs Rep T"
   312 proof -
   313   interpret type_definition Rep Abs UNIV by fact
   314   from Abs_inject Rep_inverse Abs_inverse T_def show ?thesis 
   315     by (fastforce intro!: QuotientI fun_eq_iff)
   316 qed
   317 
   318 lemma UNIV_typedef_to_equivp:
   319   fixes Abs :: "'a \<Rightarrow> 'b"
   320   and Rep :: "'b \<Rightarrow> 'a"
   321   assumes "type_definition Rep Abs (UNIV::'a set)"
   322   shows "equivp (op=::'a\<Rightarrow>'a\<Rightarrow>bool)"
   323 by (rule identity_equivp)
   324 
   325 lemma typedef_to_Quotient:
   326   assumes "type_definition Rep Abs S"
   327   and T_def: "T \<equiv> (\<lambda>x y. x = Rep y)"
   328   shows "Quotient (invariant (\<lambda>x. x \<in> S)) Abs Rep T"
   329 proof -
   330   interpret type_definition Rep Abs S by fact
   331   from Rep Abs_inject Rep_inverse Abs_inverse T_def show ?thesis
   332     by (auto intro!: QuotientI simp: invariant_def fun_eq_iff)
   333 qed
   334 
   335 lemma typedef_to_part_equivp:
   336   assumes "type_definition Rep Abs S"
   337   shows "part_equivp (invariant (\<lambda>x. x \<in> S))"
   338 proof (intro part_equivpI)
   339   interpret type_definition Rep Abs S by fact
   340   show "\<exists>x. invariant (\<lambda>x. x \<in> S) x x" using Rep by (auto simp: invariant_def)
   341 next
   342   show "symp (invariant (\<lambda>x. x \<in> S))" by (auto intro: sympI simp: invariant_def)
   343 next
   344   show "transp (invariant (\<lambda>x. x \<in> S))" by (auto intro: transpI simp: invariant_def)
   345 qed
   346 
   347 lemma open_typedef_to_Quotient:
   348   assumes "type_definition Rep Abs {x. P x}"
   349   and T_def: "T \<equiv> (\<lambda>x y. x = Rep y)"
   350   shows "Quotient (invariant P) Abs Rep T"
   351   using typedef_to_Quotient [OF assms] by simp
   352 
   353 lemma open_typedef_to_part_equivp:
   354   assumes "type_definition Rep Abs {x. P x}"
   355   shows "part_equivp (invariant P)"
   356   using typedef_to_part_equivp [OF assms] by simp
   357 
   358 text {* Generating transfer rules for quotients. *}
   359 
   360 context
   361   fixes R Abs Rep T
   362   assumes 1: "Quotient R Abs Rep T"
   363 begin
   364 
   365 lemma Quotient_right_unique: "right_unique T"
   366   using 1 unfolding Quotient_alt_def right_unique_def by metis
   367 
   368 lemma Quotient_right_total: "right_total T"
   369   using 1 unfolding Quotient_alt_def right_total_def by metis
   370 
   371 lemma Quotient_rel_eq_transfer: "(T ===> T ===> op =) R (op =)"
   372   using 1 unfolding Quotient_alt_def fun_rel_def by simp
   373 
   374 lemma Quotient_abs_induct:
   375   assumes "\<And>y. R y y \<Longrightarrow> P (Abs y)" shows "P x"
   376   using 1 assms unfolding Quotient_def by metis
   377 
   378 end
   379 
   380 text {* Generating transfer rules for total quotients. *}
   381 
   382 context
   383   fixes R Abs Rep T
   384   assumes 1: "Quotient R Abs Rep T" and 2: "reflp R"
   385 begin
   386 
   387 lemma Quotient_bi_total: "bi_total T"
   388   using 1 2 unfolding Quotient_alt_def bi_total_def reflp_def by auto
   389 
   390 lemma Quotient_id_abs_transfer: "(op = ===> T) (\<lambda>x. x) Abs"
   391   using 1 2 unfolding Quotient_alt_def reflp_def fun_rel_def by simp
   392 
   393 lemma Quotient_total_abs_induct: "(\<And>y. P (Abs y)) \<Longrightarrow> P x"
   394   using 1 2 assms unfolding Quotient_alt_def reflp_def by metis
   395 
   396 lemma Quotient_total_abs_eq_iff: "Abs x = Abs y \<longleftrightarrow> R x y"
   397   using Quotient_rel [OF 1] 2 unfolding reflp_def by simp
   398 
   399 end
   400 
   401 text {* Generating transfer rules for a type defined with @{text "typedef"}. *}
   402 
   403 context
   404   fixes Rep Abs A T
   405   assumes type: "type_definition Rep Abs A"
   406   assumes T_def: "T \<equiv> (\<lambda>(x::'a) (y::'b). x = Rep y)"
   407 begin
   408 
   409 lemma typedef_left_unique: "left_unique T"
   410   unfolding left_unique_def T_def
   411   by (simp add: type_definition.Rep_inject [OF type])
   412 
   413 lemma typedef_bi_unique: "bi_unique T"
   414   unfolding bi_unique_def T_def
   415   by (simp add: type_definition.Rep_inject [OF type])
   416 
   417 (* the following two theorems are here only for convinience *)
   418 
   419 lemma typedef_right_unique: "right_unique T"
   420   using T_def type Quotient_right_unique typedef_to_Quotient 
   421   by blast
   422 
   423 lemma typedef_right_total: "right_total T"
   424   using T_def type Quotient_right_total typedef_to_Quotient 
   425   by blast
   426 
   427 lemma typedef_rep_transfer: "(T ===> op =) (\<lambda>x. x) Rep"
   428   unfolding fun_rel_def T_def by simp
   429 
   430 end
   431 
   432 text {* Generating the correspondence rule for a constant defined with
   433   @{text "lift_definition"}. *}
   434 
   435 lemma Quotient_to_transfer:
   436   assumes "Quotient R Abs Rep T" and "R c c" and "c' \<equiv> Abs c"
   437   shows "T c c'"
   438   using assms by (auto dest: Quotient_cr_rel)
   439 
   440 text {* Proving reflexivity *}
   441 
   442 definition reflp' :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool" where "reflp' R \<equiv> reflp R"
   443 
   444 lemma Quotient_to_left_total:
   445   assumes q: "Quotient R Abs Rep T"
   446   and r_R: "reflp R"
   447   shows "left_total T"
   448 using r_R Quotient_cr_rel[OF q] unfolding left_total_def by (auto elim: reflpE)
   449 
   450 lemma reflp_Quotient_composition:
   451   assumes "left_total R"
   452   assumes "reflp T"
   453   shows "reflp (R OO T OO R\<inverse>\<inverse>)"
   454 using assms unfolding reflp_def left_total_def by fast
   455 
   456 lemma reflp_fun1:
   457   assumes "is_equality R"
   458   assumes "reflp' S"
   459   shows "reflp (R ===> S)"
   460 using assms unfolding is_equality_def reflp'_def reflp_def fun_rel_def by blast
   461 
   462 lemma reflp_fun2:
   463   assumes "is_equality R"
   464   assumes "is_equality S"
   465   shows "reflp (R ===> S)"
   466 using assms unfolding is_equality_def reflp_def fun_rel_def by blast
   467 
   468 lemma is_equality_Quotient_composition:
   469   assumes "is_equality T"
   470   assumes "left_total R"
   471   assumes "left_unique R"
   472   shows "is_equality (R OO T OO R\<inverse>\<inverse>)"
   473 using assms unfolding is_equality_def left_total_def left_unique_def OO_def conversep_iff
   474 by fastforce
   475 
   476 lemma left_total_composition: "left_total R \<Longrightarrow> left_total S \<Longrightarrow> left_total (R OO S)"
   477 unfolding left_total_def OO_def by fast
   478 
   479 lemma left_unique_composition: "left_unique R \<Longrightarrow> left_unique S \<Longrightarrow> left_unique (R OO S)"
   480 unfolding left_unique_def OO_def by fast
   481 
   482 lemma reflp_equality: "reflp (op =)"
   483 by (auto intro: reflpI)
   484 
   485 text {* Proving a parametrized correspondence relation *}
   486 
   487 definition POS :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool" where
   488 "POS A B \<equiv> A \<le> B"
   489 
   490 definition  NEG :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool" where
   491 "NEG A B \<equiv> B \<le> A"
   492 
   493 (*
   494   The following two rules are here because we don't have any proper
   495   left-unique ant left-total relations. Left-unique and left-total
   496   assumptions show up in distributivity rules for the function type.
   497 *)
   498 
   499 lemma bi_unique_left_unique[transfer_rule]: "bi_unique R \<Longrightarrow> left_unique R"
   500 unfolding bi_unique_def left_unique_def by blast
   501 
   502 lemma bi_total_left_total[transfer_rule]: "bi_total R \<Longrightarrow> left_total R"
   503 unfolding bi_total_def left_total_def by blast
   504 
   505 lemma pos_OO_eq:
   506   shows "POS (A OO op=) A"
   507 unfolding POS_def OO_def by blast
   508 
   509 lemma pos_eq_OO:
   510   shows "POS (op= OO A) A"
   511 unfolding POS_def OO_def by blast
   512 
   513 lemma neg_OO_eq:
   514   shows "NEG (A OO op=) A"
   515 unfolding NEG_def OO_def by auto
   516 
   517 lemma neg_eq_OO:
   518   shows "NEG (op= OO A) A"
   519 unfolding NEG_def OO_def by blast
   520 
   521 lemma POS_trans:
   522   assumes "POS A B"
   523   assumes "POS B C"
   524   shows "POS A C"
   525 using assms unfolding POS_def by auto
   526 
   527 lemma NEG_trans:
   528   assumes "NEG A B"
   529   assumes "NEG B C"
   530   shows "NEG A C"
   531 using assms unfolding NEG_def by auto
   532 
   533 lemma POS_NEG:
   534   "POS A B \<equiv> NEG B A"
   535   unfolding POS_def NEG_def by auto
   536 
   537 lemma NEG_POS:
   538   "NEG A B \<equiv> POS B A"
   539   unfolding POS_def NEG_def by auto
   540 
   541 lemma POS_pcr_rule:
   542   assumes "POS (A OO B) C"
   543   shows "POS (A OO B OO X) (C OO X)"
   544 using assms unfolding POS_def OO_def by blast
   545 
   546 lemma NEG_pcr_rule:
   547   assumes "NEG (A OO B) C"
   548   shows "NEG (A OO B OO X) (C OO X)"
   549 using assms unfolding NEG_def OO_def by blast
   550 
   551 lemma POS_apply:
   552   assumes "POS R R'"
   553   assumes "R f g"
   554   shows "R' f g"
   555 using assms unfolding POS_def by auto
   556 
   557 text {* Proving a parametrized correspondence relation *}
   558 
   559 lemma fun_mono:
   560   assumes "A \<ge> C"
   561   assumes "B \<le> D"
   562   shows   "(A ===> B) \<le> (C ===> D)"
   563 using assms unfolding fun_rel_def by blast
   564 
   565 lemma pos_fun_distr: "((R ===> S) OO (R' ===> S')) \<le> ((R OO R') ===> (S OO S'))"
   566 unfolding OO_def fun_rel_def by blast
   567 
   568 lemma functional_relation: "right_unique R \<Longrightarrow> left_total R \<Longrightarrow> \<forall>x. \<exists>!y. R x y"
   569 unfolding right_unique_def left_total_def by blast
   570 
   571 lemma functional_converse_relation: "left_unique R \<Longrightarrow> right_total R \<Longrightarrow> \<forall>y. \<exists>!x. R x y"
   572 unfolding left_unique_def right_total_def by blast
   573 
   574 lemma neg_fun_distr1:
   575 assumes 1: "left_unique R" "right_total R"
   576 assumes 2: "right_unique R'" "left_total R'"
   577 shows "(R OO R' ===> S OO S') \<le> ((R ===> S) OO (R' ===> S')) "
   578   using functional_relation[OF 2] functional_converse_relation[OF 1]
   579   unfolding fun_rel_def OO_def
   580   apply clarify
   581   apply (subst all_comm)
   582   apply (subst all_conj_distrib[symmetric])
   583   apply (intro choice)
   584   by metis
   585 
   586 lemma neg_fun_distr2:
   587 assumes 1: "right_unique R'" "left_total R'"
   588 assumes 2: "left_unique S'" "right_total S'"
   589 shows "(R OO R' ===> S OO S') \<le> ((R ===> S) OO (R' ===> S'))"
   590   using functional_converse_relation[OF 2] functional_relation[OF 1]
   591   unfolding fun_rel_def OO_def
   592   apply clarify
   593   apply (subst all_comm)
   594   apply (subst all_conj_distrib[symmetric])
   595   apply (intro choice)
   596   by metis
   597 
   598 subsection {* Domains *}
   599 
   600 lemma pcr_Domainp_par_left_total:
   601   assumes "Domainp B = P"
   602   assumes "left_total A"
   603   assumes "(A ===> op=) P' P"
   604   shows "Domainp (A OO B) = P'"
   605 using assms
   606 unfolding Domainp_iff[abs_def] OO_def bi_unique_def left_total_def fun_rel_def 
   607 by (fast intro: fun_eq_iff)
   608 
   609 lemma pcr_Domainp_par:
   610 assumes "Domainp B = P2"
   611 assumes "Domainp A = P1"
   612 assumes "(A ===> op=) P2' P2"
   613 shows "Domainp (A OO B) = (inf P1 P2')"
   614 using assms unfolding fun_rel_def Domainp_iff[abs_def] OO_def
   615 by (fast intro: fun_eq_iff)
   616 
   617 definition rel_pred_comp :: "('a => 'b => bool) => ('b => bool) => 'a => bool"
   618 where "rel_pred_comp R P \<equiv> \<lambda>x. \<exists>y. R x y \<and> P y"
   619 
   620 lemma pcr_Domainp:
   621 assumes "Domainp B = P"
   622 shows "Domainp (A OO B) = (\<lambda>x. \<exists>y. A x y \<and> P y)"
   623 using assms by blast
   624 
   625 lemma pcr_Domainp_total:
   626   assumes "bi_total B"
   627   assumes "Domainp A = P"
   628   shows "Domainp (A OO B) = P"
   629 using assms unfolding bi_total_def 
   630 by fast
   631 
   632 lemma Quotient_to_Domainp:
   633   assumes "Quotient R Abs Rep T"
   634   shows "Domainp T = (\<lambda>x. R x x)"  
   635 by (simp add: Domainp_iff[abs_def] Quotient_cr_rel[OF assms])
   636 
   637 lemma invariant_to_Domainp:
   638   assumes "Quotient (Lifting.invariant P) Abs Rep T"
   639   shows "Domainp T = P"
   640 by (simp add: invariant_def Domainp_iff[abs_def] Quotient_cr_rel[OF assms])
   641 
   642 end
   643 
   644 subsection {* ML setup *}
   645 
   646 ML_file "Tools/Lifting/lifting_util.ML"
   647 
   648 ML_file "Tools/Lifting/lifting_info.ML"
   649 setup Lifting_Info.setup
   650 
   651 lemmas [reflexivity_rule] = 
   652   reflp_equality reflp_Quotient_composition is_equality_Quotient_composition 
   653   left_total_fun left_unique_fun left_total_eq left_unique_eq left_total_composition
   654   left_unique_composition
   655 
   656 text {* add @{thm reflp_fun1} and @{thm reflp_fun2} manually through ML
   657   because we don't want to get reflp' variant of these theorems *}
   658 
   659 setup{*
   660 Context.theory_map 
   661   (fold
   662     (snd oo (Thm.apply_attribute Lifting_Info.add_reflexivity_rule_raw_attribute)) 
   663       [@{thm reflp_fun1}, @{thm reflp_fun2}])
   664 *}
   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 reflp'
   678 
   679 end